简体   繁体   English

当视图模型在MVVM中更改时,如何使视图更新?

[英]How to make the view update when the view model changes in MVVM?

I am trying to implement MVVM, but my view is not updating when the view model changes. 我正在尝试实现MVVM,但是当视图模型更改时,我的视图没有更新。 This is my view model: 这是我的视图模型:

public class ViewModelDealDetails : INotifyPropertyChanged
{
    private Deal selectedDeal;

    public Deal SelectedDeal
    {
        get { return selectedDeal; }
        set
        {
            selectedDeal = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

In my XAML for the view I have this: 在我的XAML视图中,我有以下内容:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
       <StackPanel>
           <TextBlock Text="{Binding Path=SelectedDeal.Title, Mode=TwoWay}"></TextBlock>
       </StackPanel>
</Grid>

The deal class: 交易类别:

public class Deal
{
    private string title;
    private float price;

    public Deal()
    {
        this.title = "Example";    
    }

    public Deal(string title, float price)
    {
        this.title = title;
        this.price = price;
    }

    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    public float Price
    {
        get { return price; }
        set { price = value; }
    }
}

When the application starts the value is correct, but when SelectedDeal changes, the view doesn't. 当应用程序启动时,该值是正确的,但是当SelectedDeal更改时,该视图不会更改。 What am I missing? 我想念什么?

The path of your binding is nested.To make it work, your Deal class should implement INotifyPropertyChanged too. 绑定的路径是嵌套的。要使其起作用,您的Deal类也应实现INotifyPropertyChanged Otherwise, it will not be triggered unless SelectedDeal is changed. 否则,除非更改SelectedDeal ,否则不会触发它。 I would suggest you make your view models all inherited from BindableBase . 我建议您使视图模型全部继承自BindableBase It would make your life much easier. 这会使您的生活更加轻松。

   public class ViewModelDealDetails: BindableBase
    {
        private Deal selectedDeal;

        public Deal SelectedDeal
        {
            get { return selectedDeal; }
            set { SetProperty(ref selectedDeal, value); }
        }

    }

    public class Deal: BindableBase
    {
        private string title;

        public string Title
        {
            get { return title; }
            set { SetProperty(ref title, value); }
        }
    }

The above code should work. 上面的代码应该工作。

BTW: If you have no access to the code of Deal class, then to trigger the binding you will have to recreate an instance of SelectedDeal each time when the value of Title is changed. 顺便说一句:如果您无权访问Deal类的代码,那么每次更改Title的值时,都必须重新创建SelectedDeal实例来触发绑定。

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

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