OxyPlot - WPF PlotView 不更新 属性
OxyPlot - WPF PlotView does not update property
我有一个 PlotView,其高度绑定到视图模型中的 属性 ModelHeight。
MainWindow.xaml
<oxy:PlotView x:Name="IOPlot" Model="{Binding IOPlotModel}" Height="{Binding ModelHeight, UpdateSourceTrigger=PropertyChanged}" Width="630">
ViewModel.cs
private double modelheight = 300;
public double ModelHeight {
get{return modelheight;}
set { modelheight = value;
RaisePropertyChanged("ModelHeight");
RaisePropertyChanged("IOPlotModel");
this.IOPlotModel.InvalidatePlot(false);
}
}
问题是:它没有用数据绑定更新高度!高度仅在 PlotModel 启动期间更改一次。高度似乎是固定的(IOPlotModel.Height 始终等于 300,即使 ModelHeight 已更改)。
有谁知道如何解决这个问题?
在这个最小的示例中,视图和视图模型是相同的 class MainWindow
,因此 DataContext
在构造函数中设置为 this
。通常应该是MVVM架构中的视图模型。
XAML:
<Window x:Class="PlotTest.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:oxyplot="http://oxyplot.org/wpf"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox x:Name="HeightTextBox"
Width="200"
TextChanged="HeightTextBox_TextChanged" />
<oxyplot:PlotView Grid.Row="1"
Model="{Binding MyModel, UpdateSourceTrigger=PropertyChanged}"
Height="{Binding ModelHeight, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>
后面的代码:
using OxyPlot;
using System.ComponentModel;
using System.Windows;
namespace PlotTest
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private double modelheight = 300;
public MainWindow()
{
this.DataContext = this;
this.MyModel = new PlotModel() { Background = OxyColors.AliceBlue };
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
public double ModelHeight
{
get { return modelheight; }
private set
{
modelheight = value;
this.RaisePropertyChanged("ModelHeight");
}
}
public PlotModel MyModel { get; private set; }
private void HeightTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
if (double.TryParse(this.HeightTextBox.Text, out double height))
{
this.ModelHeight = height;
}
}
private void RaisePropertyChanged(string propertyName = "")
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我有一个 PlotView,其高度绑定到视图模型中的 属性 ModelHeight。
MainWindow.xaml
<oxy:PlotView x:Name="IOPlot" Model="{Binding IOPlotModel}" Height="{Binding ModelHeight, UpdateSourceTrigger=PropertyChanged}" Width="630">
ViewModel.cs
private double modelheight = 300;
public double ModelHeight {
get{return modelheight;}
set { modelheight = value;
RaisePropertyChanged("ModelHeight");
RaisePropertyChanged("IOPlotModel");
this.IOPlotModel.InvalidatePlot(false);
}
}
问题是:它没有用数据绑定更新高度!高度仅在 PlotModel 启动期间更改一次。高度似乎是固定的(IOPlotModel.Height 始终等于 300,即使 ModelHeight 已更改)。 有谁知道如何解决这个问题?
在这个最小的示例中,视图和视图模型是相同的 class MainWindow
,因此 DataContext
在构造函数中设置为 this
。通常应该是MVVM架构中的视图模型。
XAML:
<Window x:Class="PlotTest.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:oxyplot="http://oxyplot.org/wpf"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox x:Name="HeightTextBox"
Width="200"
TextChanged="HeightTextBox_TextChanged" />
<oxyplot:PlotView Grid.Row="1"
Model="{Binding MyModel, UpdateSourceTrigger=PropertyChanged}"
Height="{Binding ModelHeight, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>
后面的代码:
using OxyPlot;
using System.ComponentModel;
using System.Windows;
namespace PlotTest
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private double modelheight = 300;
public MainWindow()
{
this.DataContext = this;
this.MyModel = new PlotModel() { Background = OxyColors.AliceBlue };
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
public double ModelHeight
{
get { return modelheight; }
private set
{
modelheight = value;
this.RaisePropertyChanged("ModelHeight");
}
}
public PlotModel MyModel { get; private set; }
private void HeightTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
if (double.TryParse(this.HeightTextBox.Text, out double height))
{
this.ModelHeight = height;
}
}
private void RaisePropertyChanged(string propertyName = "")
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}