我正在尝试动态更改 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/