c# - 在 WPF 中动态更改 Setter 值

标签 c# wpf wpftoolkit charts

我正在尝试动态更改 setter 值。即使我取得了部分成功,但仍坚持进一步发展。我的尝试是根据系列更改 wpf 工具包生成的柱形图的柱形颜色。我使用了一个单独类的静态成员来设置列数据点的背景属性的颜色。通过这种方式,只有最终分配的颜色才会设置到所有列,而不管它们是不同的系列。以下是有用的代码 spinets:

列数据点样式使用以下 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" />...

设置颜色的类:

public class MyBackColor {
    private static String _myBackColor;


    public static String myBackColor
    {
        get { return _myBackColor; }

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

在代码中设置颜色:

 //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);

非常感谢任何帮助。

最佳答案

我认为您需要 NotifyPropertyChange(为什么它被注释掉了?)并使背景成为 DynamicResource:

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

评论后:

您当前的解决方案行不通。当你执行

   myColumnSeries.DataPointStyle = myStyle;

您只存储对样式的引用,而不是副本。所以所有系列仍然共享相同的风格(最后一个)。

你必须使用类似的东西(前面不完整的答案):

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

或者您可以只设置系列的颜色,并为其他属性使用共享样式。

关于c# - 在 WPF 中动态更改 Setter 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5040521/

相关文章:

c# - 请求 Expression<Func<T, decimal>> 时防止隐式转换为 int

c# - 获取未知类型的方法并运行它 c#

c# - 在STA线程上显示忙碌指示器

c# - 使用 vscode : some sort of issue with the project file 运行测试时出错

c# - 不允许将 nvarchar 数据类型隐式转换为 varbinary (max)。使用 CONVERT 函数执行此查询

c# - 为什么我在 Datagrid 中已经有了 Columnheader?

.net - 命名为 RowDefinition 的网格无法作为 WPF4 中的 ItemsPanel 填充

wpf - 在WPF MVVM Prism应用程序中使用OnPropertyChanged和发布之间有什么区别?

wpf - 当 UI 值更改时,将 ViewModel 上的结构属性绑定(bind)到 WPF 工具包 PropertyGrid 不会更新 ViewModel