我正在使用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
本身显示相同的颜色。因此,我已经使用以下样式设置了图例项目
<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>
我的问题是,如何将“Rectangle.Fill”在“TestSuiteLegendItemStyle”样式中“绑定”,以使其颜色与上面定义的“LineDataPointStyle”设置的“LineSeries”颜色相同?感谢您的时间。
编辑。如下所示,我尝试了设置一个保存我的图的“Background”颜色的“DependencyProperty”。
<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(Dependency Property)被定义为
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>
希望你能提供帮助...