When ScottPlot WPF control is placed inside a data template and used for plotting, nothing gets rendered. I'm puzzled as to why the following code would not work:
This is my view:
<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>
This is my view model:
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");
}
}
}
When DataPlot
of the view model is used for plotting, nothing appears.
Scott Plot is not implemented as a proper WPF control that supports data-binding and MVVM.
There is a similar issue on GitHub that describes what you can do.
As the author already states, this is a bad pattern as your view model will contain a UI control. However, there is currently no support for data-binding for
WpfPlot
. According to the issue, although breaking MVVM, this works:You could of course derive a custom control and adapt it or use other workarounds for binding, but I do not think that is advisable, as the control itself does offcially not offer any support for this anyway.