WPF 图表工具包。将图例内容绑定到 ColumnSeries ViewModel
WPF Chart Toolkit. Bind Legend content to ColumnSeries ViewModel
我在 ColumnSeries
中有一个 LegendItemStyle
<chartingToolkit:ColumnSeries DependentValuePath="Value" IndependentValuePath="Version" ItemsSource="{Binding ColumnValues}" IsSelectionEnabled="True">
<chartingToolkit:ColumnSeries.LegendItemStyle>
<Style TargetType="chartingToolkit:LegendItem" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type chartingToolkit:LegendItem}">
<Border BorderBrush="Black" BorderThickness="0">
<StackPanel>
<StackPanel Orientation="Horizontal" >
<Rectangle Width="12" Height="12" Fill="{DynamicResource DesktopBrush}" StrokeThickness="1" />
<visualizationToolkit:Title Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type chartingToolkit:ColumnSeries}},Path=DataContext.ColumnValues.Legend1, Mode=TwoWay}" Foreground="{Binding ElementName=chart,Path=Tag}" FontSize="18" Margin="10"/>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</chartingToolkit:ColumnSeries.LegendItemStyle>
...
</chartingToolkit:ColumnSeries>
ColumnSeries
ItemsSource
绑定到下面class
private ExtendedCollection columnValues = new ExtendedCollection();
public ExtendedCollection ColumnValues
{
get
{
return columnValues;
}
set
{
columnValues = value;
PropChanged("ColumnValues");
}
}
public class ExtendedCollection : ObservableCollection<ColumnInfo>
{
public string Legend1 { get; set; }
public string Legend2 { get; set; }
}
public class ColumnInfo
{
public string Version { get; set; }
public string DateStamp { get; set; }
public int Value { get; set; }
public Brush BackgroundBrush { get; set; }
public string Platform { get; set; }
}
我无法将 LegendItem
的 Title
Content
绑定到 ViewModelColumnValues
的 属性 Legend1
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type chartingToolkit:ColumnSeries}},Path=DataContext.ColumnValues.Legend1, Mode=TwoWay}
尝试绑定到父 Legend
元素的 DataContext
:
xmlns:dataVisualization="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
...
<visualizationToolkit:Title Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dataVisualization:Legend}},Path=DataContext.ColumnValues.Legend1, Mode=TwoWay}"/>
我在 ColumnSeries
LegendItemStyle
<chartingToolkit:ColumnSeries DependentValuePath="Value" IndependentValuePath="Version" ItemsSource="{Binding ColumnValues}" IsSelectionEnabled="True">
<chartingToolkit:ColumnSeries.LegendItemStyle>
<Style TargetType="chartingToolkit:LegendItem" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type chartingToolkit:LegendItem}">
<Border BorderBrush="Black" BorderThickness="0">
<StackPanel>
<StackPanel Orientation="Horizontal" >
<Rectangle Width="12" Height="12" Fill="{DynamicResource DesktopBrush}" StrokeThickness="1" />
<visualizationToolkit:Title Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type chartingToolkit:ColumnSeries}},Path=DataContext.ColumnValues.Legend1, Mode=TwoWay}" Foreground="{Binding ElementName=chart,Path=Tag}" FontSize="18" Margin="10"/>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</chartingToolkit:ColumnSeries.LegendItemStyle>
...
</chartingToolkit:ColumnSeries>
ColumnSeries
ItemsSource
绑定到下面class
private ExtendedCollection columnValues = new ExtendedCollection();
public ExtendedCollection ColumnValues
{
get
{
return columnValues;
}
set
{
columnValues = value;
PropChanged("ColumnValues");
}
}
public class ExtendedCollection : ObservableCollection<ColumnInfo>
{
public string Legend1 { get; set; }
public string Legend2 { get; set; }
}
public class ColumnInfo
{
public string Version { get; set; }
public string DateStamp { get; set; }
public int Value { get; set; }
public Brush BackgroundBrush { get; set; }
public string Platform { get; set; }
}
我无法将 LegendItem
的 Title
Content
绑定到 ViewModelColumnValues
的 属性 Legend1
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type chartingToolkit:ColumnSeries}},Path=DataContext.ColumnValues.Legend1, Mode=TwoWay}
尝试绑定到父 Legend
元素的 DataContext
:
xmlns:dataVisualization="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
...
<visualizationToolkit:Title Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dataVisualization:Legend}},Path=DataContext.ColumnValues.Legend1, Mode=TwoWay}"/>