简体   繁体   中英

MVC through C# / WPF: how to notify view?

I'm doing a very simple implementation of the MVC pattern in C# with a WPF interface.

I have a model that's keeping the state. I want to be able to notify the view form the model whenever anything about the state changes, so that the view can update itself accordingly.

What's the simplest best practice for doing this in WPF? I know there's such a thing as a PropertyChanged event, is that what I'm looking for or is that too specific for my situation?

Thanks!

Yes. Implement the interface INotifyPropertyChanged.

An example:

MainWindow.xaml

<Window x:Class="INotifyChangedDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Label Content="{Binding HitCount}"></Label>
    <Button Grid.Row="1" Click="Button_Click">
        Hit
    </Button>
</Grid>


MainWindow.xaml.cs

namespace INotifyChangedDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainViewModel _viewModel = new MainViewModel();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = _viewModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.HitCount = _viewModel.HitCount + 1;
        }
    }
}

MainViewModel.cs

namespace INotifyChangedDemo
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private int _hitCount;
        public int HitCount
        {
            get
            {
                return _hitCount;
            }
            set
            {
                if (_hitCount == value)
                    return;

                _hitCount = value;
                // Notify the listeners that Time property has been changed
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("HitCount"));
                }

            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

For better implementation of INotifyChangedProperty, please refer to this thread: Automatically INotifyPropertyChanged .

If you wanna know more about the MVVM pattern, please see here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

If your view binds to a property declared in your model, and your property raises the PropertyChanged event whenever it is changed, then your view will automatically be updated with the new value. For instance, your view might declare the following:

<TextBlock Text="{Binding Name}" />

And in your model you would have:

string _name;
public string Name
{
    get
    {
        return _name;
    }
    set
    {
        _name = value;
        RaisePropertyChanged("Name");
    }
}

This assumes that you are using some framework / helper that provides the RaisePropertyChanged method. I am taking this example from the Galasoft MVVM framework, but I assume that exactly the same principal applies in MVC.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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