简体   繁体   English

OxyPlot - WPF PlotView 不更新属性

[英]OxyPlot - WPF PlotView does not update property

I have a PlotView that has the height binds to the property ModelHeight in the view model.我有一个 PlotView,其高度绑定到视图 model 中的属性 ModelHeight。

MainWindow.xaml主窗口.xaml

<oxy:PlotView x:Name="IOPlot" Model="{Binding IOPlotModel}" Height="{Binding ModelHeight, UpdateSourceTrigger=PropertyChanged}" Width="630">

ViewModel.cs ViewModel.cs

private double modelheight = 300;
public double ModelHeight { 
        get{return modelheight;}
        set { modelheight = value;
        RaisePropertyChanged("ModelHeight");
        RaisePropertyChanged("IOPlotModel");
        this.IOPlotModel.InvalidatePlot(false); 
        }
    }

The problem is that: it doesn't update the height with the data binding.问题是:它不使用数据绑定更新高度。 The height only changes once during the startup of the PlotModel.高度仅在 PlotModel 启动期间更改一次。 It seems like the height is fixed (IOPlotModel.Height always equals 300 even though ModelHeight changed)?似乎高度是固定的(IOPlotModel.Height 始终等于 300,即使 ModelHeight 已更改)? Does anyone know how to fix this issue?有谁知道如何解决这个问题?

In this minimal example view and view model are the same class MainWindow , so the DataContext is set to this inside the constructor.在这个最小的示例视图和视图 model 是相同的 class MainWindow ,因此DataContext在构造函数中设置this Usually it should be the view model in a MVVM architecture.通常它应该是 MVVM 架构中的视图 model。

XAML: 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>

Code behind:代码背后:

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));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM