【发布时间】:2014-01-26 19:00:36
【问题描述】:
我正在通过命名空间使用 WPF 图表工具包:
xmlns:ChartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:VisualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
我有一个图表控件,每次进行Lineseries 绘图时,我都会在其中生成一个随机颜色。我删除数据点标记并使用以下样式着色
<Style x:Key="LineDataPointStyle"
TargetType="ChartingToolkit:LineDataPoint">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Width" Value="NaN"/>
<Setter Property="Height" Value="NaN"/>
<Setter Property="Background"
Value="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource ColorBrushConverter}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ChartingToolkit:LineDataPoint">
<Grid x:Name="Root" Opacity="0"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
LineSeries 是通过
<ChartingToolkit:LineSeries Title="{Binding DisplayName}"
AnimationSequence="FirstToLast"
SnapsToDevicePixels="True"
DataPointStyle="{StaticResource LineDataPointStyle}"
ItemsSource="{Binding Data}"
IndependentValueBinding="{Binding X}"
DependentValueBinding="{Binding Y}"/>
现在,这可以正常工作,但图例标记显示的颜色与我为LineSeries 生成的随机颜色不同。我希望为 LineSeries 和 Lineseries 本身的图例项显示相同的颜色。因此,我将图例项设置为如下
<Style x:Key="TestSuiteLegendItemStyle"
TargetType="{x:Type ChartingToolkit:LegendItem}">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ChartingToolkit:LegendItem}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal">
<Rectangle Width="8"
Height="8"
Fill="{Binding Background}"
Stroke="{Binding BorderBrush}"
StrokeThickness="1" Margin="0,0,3,0" />
<VisualizationToolkit:Title Content="{TemplateBinding Content}" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的问题是我怎样才能在TestSuiteLegendItemStyle 样式中“绑定”Rectangle.Fill,使其与上面定义的LineDataPointStyle 设置的LineSeries 颜色相同?*
感谢您的宝贵时间。
编辑。我已经尝试设置一个DependencyProperty 来保存我的情节的Background 颜色,如下所示通过
<Style x:Key="LineDataPointStyle"
TargetType="ChartingToolkit:LineDataPoint">
...
<Setter Property="Background"
Value="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource ColorBrushConverter}}"/>
...
</Style>
我已经修改了转换器(如标记),以便我可以存储随机生成的背景颜色,然后在我的图例中使用它
public class ColorToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
Brush b = new SolidColorBrush(Utils.GenerateRandomColor());
MultiChartExtensions.BackgroundBrushProperty = b; <= how to set the dependency property?
return b;
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
依赖属性 (DP) 定义为
public static readonly DependencyProperty BackgroundBrushProperty;
public static void SetBackgroundBrush(DependencyObject DepObject, Brush value)
{
DepObject.SetValue(BackgroundBrushProperty, value);
}
public static Brush GetBackgroundBrush(DependencyObject DepObject)
{
return (Brush)DepObject.GetValue(BackgroundBrushProperty);
}
然后我希望通过这个 DP 设置图例背景
<Style x:Key="TestSuiteLegendItemStyle"
TargetType="{x:Type ChartingToolkit:LegendItem}">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ChartingToolkit:LegendItem}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal">
<Rectangle Width="8"
Height="8"
Fill="{Binding MultiChart:MultiChartExtensions.BackgroundBrushProperty}"
Stroke="{Binding BorderBrush}"
StrokeThickness="1" Margin="0,0,3,0" />
<VisualizationToolkit:Title Content="{TemplateBinding Content}" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
对此的任何帮助将不胜感激......
【问题讨论】:
标签: c# wpf mvvm binding charts