使用数据模板时 ScottPlot 不绘制

ScottPlot does not plot when data template is used

当 ScottPlot WPF 控件放置在数据模板内并用于绘图时,不会呈现任何内容。我对为什么以下代码不起作用感到困惑:

这是我的观点:

<Window x:Class="Client.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Client"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <DataTemplate x:Key="DataPlotTemplate">
            <StackPanel>
                <TextBlock Text="{Binding Title}"/>
                <WpfPlot MinHeight="300" MinWidth="300" Content="{Binding DataPlot}"/>
                <TextBlock Text="{Binding Description}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <ContentControl Content="{Binding DataPlotVm0}"
                            ContentTemplate="{StaticResource DataPlotTemplate}"/>
            <ContentControl Content="{Binding DataPlotVm1}"
                            ContentTemplate="{StaticResource DataPlotTemplate}"/>
        </StackPanel>
    </Grid>
</Window>

这是我的视图模型:

public class DataPlotViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChange(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private string title = "";
    public string Title
    {
        get { return title; }
        set
        {
            title = value;
            OnPropertyChange("Title");
        }
    }

    public ScottPlot.WpfPlot DataPlot { get; set; } = new ScottPlot.WpfPlot();

    private string description = "";
    public string Description
    {
        get { return description; }
        set
        {
            description = value;
            OnPropertyChange("Description");
        }
    }
}

当视图模型的DataPlot用于绘图时,没有出现。

Scott Plot not implemented as a proper WPF control 支持数据绑定和 MVVM。

ScottPlot aims to be easy to use by data scientists new to C#, so its API favors the simplicity of one-line method calls (with optional, named arguments) and intentionally avoids complex paradigms (data binding, MVVM, inheritance) commonly seen in similar libraries available for the .NET platform.

有一个类似的issue on GitHub that describes what you can do.

You can create WpfPlot in ViewModel [...] and bind it in you view [...]
It's bad pattern bring control to VM, but it should work.

正如作者已经指出的那样,这是一个糟糕的模式,因为您的视图模型将包含一个 UI 控件。但是,目前不支持 WpfPlot 的数据绑定。根据 issue,虽然打破了 MVVM,但这是有效的:

<DataTemplate x:Key="DataPlotTemplate">
   <StackPanel>
      <TextBlock Text="{Binding Title}"/>
      <ContentControl MinHeight="300" MinWidth="300" Content="{Binding DataPlot}"/>
      <TextBlock Text="{Binding Description}"/>
   </StackPanel>
</DataTemplate>

您当然可以派生一个自定义控件并对其进行调整或使用其他变通方法进行绑定,但我认为这不是可取的,因为该控件本身官方不对此提供任何支持。