c# - WPF 图表工具包。将图例内容绑定(bind)到 ColumnSeries ViewModel

标签 c# wpf mvvm wpftoolkit

我有一个 LegendItemStyleColumnSeries

<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绑定(bind)到以下类
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; }
}

我无法绑定(bind) LegendItemTitle Content到属性(property)Legend1ColumnValues在 View 模型中
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type chartingToolkit:ColumnSeries}},Path=DataContext.ColumnValues.Legend1, Mode=TwoWay}

最佳答案

尝试绑定(bind)到 DataContext parent 的Legend元素:

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}"/>

关于c# - WPF 图表工具包。将图例内容绑定(bind)到 ColumnSeries ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46426839/

相关文章:

c# - 链接按钮打开一个模态弹出窗口,但弹出窗口立即关闭

c# - 系统.AccessViolationException :Attempted to read or write protected memory

c# - 使用 C# .NET 调用 Paypal 自适应支付 API?最好使用 WebServices

c# - 如何在 WPF 数据网格中只显示日期而不显示日期和时间?

c# - 如何使用mvvm在WPF中的DataGrid中的单元格中进行IsReadOnly更改

c# - 将批处理事件发送到 azure eventhub 时失败

c# - Kinect SDK 1.7 - 如何用双手工作

wpf - 在自定义控件中未调用OnApplyTemplate

c# - ViewModel 的数据更改时,文本框不会更新

javascript - 无法使用 "data-bind=source:"将 Kendo Mobile ListView 绑定(bind)到数据