如果模型已更改,请更新ViewModel

时间:2015-11-27 08:16:13

标签: c# wpf mvvm

如何在模型更改时更新视图模型。我找到的第一个解决方案是在ViewModel中订阅Model的PropertyChangedEvent。但这是好方法吗?也许它存在更好的Notify ViewModel方式?

的Xaml:

<Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="{Binding SomeValue}"/>
        <Label Grid.Row="1" Content="{Binding Second}"/>
    </Grid>
</Grid>

代码:

namespace WpfApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}
public class ViewModel : INotifyPropertyChanged
{
    Model model = new Model();
    public ViewModel()
    {
        model.PropertyChanged += (o, e) =>
        {
            PropertyChanged(this, new PropertyChangedEventArgs(e.PropertyName));
        };
    }
    public int SomeValue
    {
        get
        {
            return this.model.SomeValue;
        }

        set
        {
            this.model.SomeValue = value;
        }
    }

    public int Second
    {
        get
        {
            return this.model.Second;
        }

        set
        {
            this.model.Second = value;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public class Model : INotifyPropertyChanged
{
    private int someValue;

    public int SomeValue
    {
        get
        {
            return this.someValue;
        }

        set
        {
            this.someValue = value;
            this.RaisePropertyChanged("SomeValue");
        }
    }

    private int second;

    public int Second
    {
        get
        {
            return this.second;
        }

        set
        {
            this.second = value;
            this.RaisePropertyChanged("Second");
        }
    }

    public Model()
    {
        Action Test = new Action(WorkAsync);
        IAsyncResult result = Test.BeginInvoke(null,null);
    }

    public void WorkAsync()
    {
        while(true)
        {
            System.Threading.Thread.Sleep(3000);
            SomeValue += 1;
            Second += 1;
            RaisePropertyChanged("SomeValue");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

创建raisepropertychanged方法/调用它

在控件上强制执行双向绑定

look here

and here and here is best

xaml two way binding

答案 1 :(得分:0)

您的模型不一定需要来实现INotifyPropertyChanged MVVM方法将在视图中实现INotifyPropertyChanged模型并仅公开模型的视图需要的属性。

在您的情况下,它看起来像这样:

public class Model 
{
    public int SomeValue { get; set; }
}

public ViewModel : INotifyPropertyChanged
{
    private Model _model = new Model();

    public int SomeModelValue
    {
        get { return _model.SomeValue; }
        set 
        {
            _model.SomeValue = value;
            //Notify property changed
        }
    }
    ...
}

您的视图将绑定到SomeModelValue

<TextBox Text="{Binding SomeModelValue}" ... />

此方法的好处是您的视图模型将整个模型公开给视图,并且只有看到的模型的属性才会被显示观点。它还允许您的模型保持相对哑。