Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a Setter Value Dynamically in WPF

I am trying to change a setter value dynamically. Even though I am successful partially, stuck with progressing further. My attempt is to change the column colours of a wpf toolkit generated column chart depending on the series. I used a static member of a separate class to set the color of the background property of the column data point. In this way only the finally assigned color gets set to all the columns regardless of they being different series. Following are the useful code spinets:

Column datapoint style is defined with the following xaml:

<local:MyBackColor x:Key="mybackresource"></local:MyBackColor>
<Style x:Key="InfocruiserAquaColumn" TargetType="DVC:ColumnDataPoint">
    <Setter Property="Background" Value="{Binding Source={StaticResource mybackresource}, Path= myBackColor}"/>
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1" />...

Class which sets the color:

public class MyBackColor {
    private static String _myBackColor;


    public static String myBackColor
    {
        get { return _myBackColor; }

        set
        {
            _myBackColor = value;
            //Helpers.InvokePropertyChanged(PropertyChanged, this, "Grade");
        }
    }
}

Setting the color in code:

 //Changing the column color
                MyBackColor.myBackColor = colorValue;

                myStyle = (Style)FindResource("InfocruiserAquaColumn");

                ColumnSeries myColumnSeries = new ColumnSeries();
                myColumnSeries.Title = "Experience";

                myColumnSeries.ItemsSource = seriesData;

                myColumnSeries.IndependentValueBinding = new Binding("Key");
                myColumnSeries.DependentValueBinding = new Binding("Value");
                myColumnSeries.Background = new SolidColorBrush(Colors.Black);


                myColumnSeries.DataPointStyle = myStyle;

                mcChart.Series.Add(myColumnSeries);

Any help is deeply appreciated.

like image 879
picmate 涅 Avatar asked Sep 12 '26 20:09

picmate 涅


1 Answers

I think you need both the NotifyPropertyChange (why is it commented out?) and to make the Background a DynamicResource:

<Setter Property="Background" 
      Value="{Binding Source={DynamicResource mybackresource}, Path= myBackColor}"/>

After the comments:

Your current solution is not going to work. When you execute

   myColumnSeries.DataPointStyle = myStyle;

you are only storing a reference to the style, not a copy. So all Series still share the same Style (the last one).

You'll have to use something like (incomplete answer ahead):

myColumnSeries.DataPointStyle = new Style();
myColumnSeries.DataPointStyle.Background = ...

Or maybe you can set just the Color of the Series and use a shared Style for the other properties.

like image 107
Henk Holterman Avatar answered Sep 15 '26 10:09

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!