简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM