当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
用于绘图时,什么都不会出现。
发布于 2020-12-09 08:36:08
Scott是支持数据绑定和MVVM的未作为适当的WPF控件实现。。
ScottPlot的目标是让新加入C#的数据科学家更容易使用,因此它的API倾向于单行方法调用(带有可选的命名参数)的简单性,而有意避免复杂的范例(数据绑定、MVVM、继承)--通常出现在类似于.NET平台的库中。
有一个类似的关于GitHub的问题描述你能做什么。
您可以在WpfPlot中创建ViewModel ..。把它绑在你的视野里..。 --它的坏模式给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>
当然,您可以派生自定义控件并对其进行调整或使用其他绑定解决方案,但我认为这是不可取的,因为控件本身并不提供对此的任何支持。
https://stackoverflow.com/questions/65203955
复制相似问题