【问题标题】:GUI is not updated by using Two way binding in wpf在 wpf 中使用双向绑定不会更新 GUI
【发布时间】:2023-03-29 07:45:01
【问题描述】:

我正在开发使用 MVVM 模式的项目。

我有一个文本框:

<TextBox Height="23" HorizontalAlignment="Right" Margin="0,30,72,0" Name="textBox1" 
         Text="{Binding Path=Rspchange, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" VerticalAlignment="Top" Width="75" />

和标签:

<Label Content="{Binding Path=LF, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="24" HorizontalAlignment="Left" Margin="540,3,0,0" Name="lbLF" VerticalAlignment="Top" Width="84" />

这里我分别使用了属性 Rspchange 和 LF 的两种方式绑定。

public UInt16 Rspchange
{
    get
    {
        return this.eObj.GetRsBaseline(this.index);
    }
    set
    {
        this.eObj.SetRsBaseline(value, this.Index);
        this.OnPropertyChanged(new PropertyChangedEventArgs("Rspchange"));
        this.eObj.writedata(DefaultFileName);
    }
} 

public string LF
{
    get
    {
        return this.eObj.LFrequency;
    }
    set
    {
        this.eObj.LFrequency = value;
        this.OnPropertyChanged(new PropertyChangedEventArgs(" LF"));
    }
}

在模型方面,我有这样的属性:

public override string LFrequency
{
    get { return configObject.lnFrequency.ToString(); }
    set { configObject.lnFrequency = Convert.ToByte(value); }
}

public override UInt16 GetRsBaseline(int channelIndex)
{
    return (byte)this.configObject.CConfig[index].Bline;
}

public override void SetRsBaseline(UInt16 value, int index)
{
    byte[] arr = BitConverter.GetBytes(value);
    this.configObject.CConfig[index].Bline = arr[0];
}

public byte Bline
{
    get { return this.bline; }
    set { this.bline = value; }
}

public byte lnFrequency
{
    get { return this.ln_frequency; }
    set { this.ln_frequency = value; }
}

我的应用程序正在通过串行端口与其他应用程序通信。我正在从其他应用程序获取数据,因此我还必须更新我的 GUI。

当我调试我的代码时,我发现我正在从其他应用程序获取更新值到 ln_frequency 和 bline,但它没有更新我的 GUI。

请告诉我我的代码有什么问题,以便在我从其他应用程序获取数据后更新我的 GUI。

P.S:writexmldata的代码

public void WriteXmlData(string path)
{
    if ((ie.Current as XElement).Attribute("name").Value == "Bline")
    {
        (ie.Current as XElement).Attribute("value").Value = this.channel_config[0].Bline.ToString();
    }

    if ((ie.Current as XElement).Attribute("name").Value == "LFrequency")
    {
        (ie.Current as XElement).Attribute("value").Value = this.lnFrequency.ToString();
    }
}

这里我的意图是保存 Bline 和 LFrequency 的当前值,以便当用户再次启动我的应用程序时,用户将始终在 GUI 上获得最新值。

【问题讨论】:

    标签: xml wpf xaml mvvm inotifypropertychanged


    【解决方案1】:

    那么,你有你的 xaml 视图、一个包含 Rspchange 和 LF 属性的 ViewModel,以及一个包含 LFrequency、Bline 和 lnFrequency 属性的模型。

    您了解 Rspchange 上的更新将更新您的 UI,因为这行:

    this.OnPropertyChanged(new PropertyChangedEventArgs("Rspchange"));
    

    它基本上对 UI 说:“嘿,如果你在乎,我更新我的 Rspchange 属性”。由于存在 2 路绑定,UI 会从您的 viewModel 中读取新值。

    现在很明显,如果您更新 lnFrequency 或 Bline,则不会更新任何内容,因为它们的设置器中没有通知。

    我认为你在 MVVM 的实现上跳了几步,这篇文章应该可以帮助你改进它:WPF MVVM INotifyPropertyChanged Implementation - Model or ViewModel

    总之,您需要一种方法让您的模型通知您的 ViewModel 它已更新。但是不使用 INotifyPropertyChanged 会更符合 MVVM 的要求。因此,您可以考虑 lnFrequency 和 Bline 实际上是您的 viewModel 的一部分,或者为它们创建一个新的 ViewModel,或者使用任何类型的经典事件在您的解决方案中传播“由外部系统更新的模型”信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-12
      • 2014-04-10
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 2015-09-15
      • 1970-01-01
      相关资源
      最近更新 更多