0

当 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视图模型用于绘图时,什么都不会出现。

4

1 回答 1

2

Scott Plot未实现为支持数据绑定和 MVVM 的适当 WPF 控件。

ScottPlot 旨在让 C# 的新数据科学家易于使用,因此它的 API 支持单行方法调用的简单性(带有可选的命名参数),并有意避免类似的复杂范例(数据绑定、MVVM、继承) .NET 平台可用的库。

GitHub 上有一个类似的问题,描述了您可以做什么

您可以在 ViewModel [...] 中创建 WpfPlot 并将其绑定到您的视图 [...]
将控制权交给 VM 是不好的模式,但它应该可以工作。

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

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

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

于 2020-12-09T08:36:08.893 回答