CartesianChart(Live-Charts)不适用于DataTemplate

本文关键字:适用于 DataTemplate 不适用 Live-Charts CartesianChart | 更新日期: 2023-09-27 17:56:30

我使用实时图表库,但我在显示图表时遇到问题。事实上,在我的窗口中,我想要许多具有多种类型的图表,所以我使用 DataTemplateSelector,链接到每种图表类型的类。但是,当图表出现时,上面没有数据。我试图在我的"柱形图"的声明中添加DataContext="{Binding}"但没有成功。

这是我的代码摘录:

ResultView.xaml

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    <FrameworkElement.Resources>
        <DataTemplate x:Key="graphEmptyTemlpate">
            <StackPanel Width="Auto" Height="60" Orientation="Horizontal" MaxWidth="700">
                <TextBlock Text="{Binding name}" VerticalAlignment="Center" TextAlignment="Center"  FontSize="20" Margin="0,0,0,0" Foreground="#FF006B93"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="columnTemplate">
            <StackPanel Background="#FFBFBFBF">
                <TextBlock Text="{Binding title}"/>
                <TextBlock Text="{Binding subTitle}"/>
                <lvc:CartesianChart Width="400" Height="400" Series="{Binding listSeries}">
                </lvc:CartesianChart>
            </StackPanel>
        </DataTemplate>
        <local:GraphTemplateSelector
        ColumnTemplate="{StaticResource columnTemplate}"
        GraphEmptyTemplate="{StaticResource graphEmptyTemlpate}"
        x:Key="graphTemplateSelector" />
    </FrameworkElement.Resources>
    <StackPanel Orientation="Horizontal" >
        <ItemsControl ItemsSource="{Binding Results}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Expander Header="{Binding Title}"
                              IsExpanded="True"
                              Margin="10 10 10 10"
                              FontSize="20"
                              Foreground="White">
                        <ItemsControl ItemsSource="{Binding Items}" 
                                        ItemTemplateSelector="{DynamicResource graphTemplateSelector}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel Margin="10 10 10 10" Orientation="Vertical"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </Expander>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</ScrollViewer>

我的 ViewModel 类的构造函数位于 ResultViewModel 中.cs在 xaml 中,结果对应于 c# 中的结果,项对应于 RpAgitation。c# 中的项

public ResultsViewModel()
{
    instance = this;
    this.Results = new ObservableCollection<ResultParts>();
    this.RpAgitation = new ResultParts("Results of the Agitation part");
    ColumnGraph gr = new ColumnGraph();
    RpAgitation.Items.Add(gr);
    this.Results.Add(_rpAgitation);
}

以及柱图.cs

public class ColumnGraph : IGraph
{
    public SeriesCollection listSeries { get; set; }
    public ColumnGraph()
    {
        listSeries = new SeriesCollection
        {
            new LineSeries
            {
                Title = "Series 1",
                Values = new ChartValues<double> { 4,7,8,9,5,4,2}
            }
        };
    }
}

感谢您的帮助,

CartesianChart(Live-Charts)不适用于DataTemplate

目前 SeriesCollection 类不是 WPF UiFramework 元素,我真的不想这样做,但我想我们不需要这样做来避免这个问题。

我不确定这是WPF的错误,还是我们做错了。

无论如何,我将进一步调查,现在另一种方法是尝试以下语法:

DataSource = new List<ChartValues<double>>
        {
            new ChartValues<double> {r.Next(0, 10), r.Next(0, 10), r.Next(0, 10)},
            new ChartValues<double> {r.Next(0, 10), r.Next(0, 10), r.Next(0, 10)},
            new ChartValues<double> {r.Next(0, 10), r.Next(0, 10), r.Next(0, 10)}
        };
        DataContext = this;

和 XAML

<ItemsControl ItemsSource="{Binding DataSource}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <lvc:CartesianChart Height="400">
                        <lvc:CartesianChart.Series>
                            <lvc:LineSeries Values="{Binding}"></lvc:LineSeries>
                        </lvc:CartesianChart.Series>
                    </lvc:CartesianChart>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>