我无法更新我的绑定数据(仅在第一次更新)

时间:2021-03-02 15:13:57

标签: c# wpf

我想将流数据实时显示到文本框。但是即使流数据已更新,文本框也不会更新。我不知道出了什么问题。

这是我的 XAML 代码。

<TextBox Text="{Binding Path = marketPrice}" HorizontalAlignment="Left"/>

这是查看模型代码。

public class OrderTestViewModel : INotifyPropertyChanged
{
     public QuotesDataSource DataSource;
     public string _marketPrice => DataSource.SymbolPrice;
     public string marketPrice
     {
         get { return _marketPrice; }
         set
         {
             RaisePropertyChanged("marketPrice");
         }
     }  
     public event PropertyChangedEventHandler PropertyChanged;
     public void RaisePropertyChanged(string propertyName)
     {
         if (PropertyChanged != null)
             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }   
}

我检查过市场价格是实时更新的。

最后一个是隐藏代码。

public partial class OrderTest : UserControl
{
   OrderTestViewModel model = new OrderTestViewModel();
   public OrderTest()
   {
       InitializeComponent();
       DataContext = model;
   }
}

请帮帮我。

2 个答案:

答案 0 :(得分:0)

您的 marketPrice 设置器似乎从未更新 _marketPrice 的值(它将始终显示相同的值。

你想要这样的东西:

This app collects background location data even when the app is closed, to enable:
    \n - distance calculation between courier and restaurant or client
    \n - restrict order assignment and delivery based on that distance

答案 1 :(得分:-1)

<块引用>

我想在更新 DataSource.SymbolPrice 时更新 UI 中的新值。 DataSource.SymbolPrice 定期更新

然后您应该直接绑定到 SymbolPrice 属性并实现 INotifyPropertyChanged 并在 PropertyChanged 类中引发 QuotesDataSource 事件:

public class OrderTestViewModel
{
    public QuotesDataSource DataSource;

    public string marketPrice => DataSource.SymbolPrice;
}

显然,某些对象必须告诉 UI 何时有更新,这是源对象的责任。

视图模型不应该知道 QuotesDataSource 中的价格何时发生变化,除非后者以某种方式告诉它,例如通过引发事件。