wpf - use of DependencyObject to implement data model

本文介绍了一种使用DependencyObject作为数据模型基类的方法,并对比了INotifyPropertyChanged接口的实现方式。通过具体的代码示例展示了如何定义和使用依赖属性来实现数据绑定及通知更新的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The most common practise to implement the DataModel is by declaring the class to implements the INotifyPropertyChanged interface. 

 

However, it is one of the many ways that you can implement data model, an alternative way is to use DependencyObject. 

 

The rationale behind DependencyObject as the base class to implement data model is that it 

 

  1. It has SetValue and GetValue to operate on those DependencyProperty
  2. It has the OnPropertyChanged(DependencyPropertyChangedEventArgs) to notify an update is on going. 

 

 

We can define our Data Model as follow.

 

    public string Message
    {
      get { return (string)GetValue(MessageProperty); }
      set {
        SetAndRaiseProeprtyChanged(MessageProperty, value);
        //SetValue(MessageProperty, value); 
      }
    }

    // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("Message", typeof(string), typeof(DataModel), new UIPropertyMetadata(string.Empty));


    private void SetAndRaiseProeprtyChanged(DependencyProperty dp, object newValue)
    {
      var oldvalue = GetValue(dp);
      SetValue(dp, newValue);
      OnPropertyChanged(new DependencyPropertyChangedEventArgs(dp, oldValue: oldvalue, newValue: newValue));
    }

  }
 

and suppose that there is a view which consumes the data model, here is the view (window)

 

<Window x:Class="DependencyObjectDemo1.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">
    <StackPanel>
        <TextBlock Name="textBlock" Text="{Binding Message}" />
        <Button Click="ChangeDataModel" Content="Click me to change DataModel"/>
    </StackPanel>
</Window>
 

and here is the code that connect the data model and the view. 

 

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      InitializeDataModel();
    }

    public void InitializeDataModel()
    {
      var dataModel = new DataModel
      {
        Message = "Hello world"
      };

      this.DataContext = dataModel;
    }

    private void ChangeDataModel(object sender, RoutedEventArgs e)
    {
      var dataModel = this.DataContext as DataModel;
      if (dataModel != null)
      {
        dataModel.Message = "New Message";
      }
    }
  }
 

 

 

CAVEAT: it has been discussed on some other thread about the performance/memory hit by introducing the DependencyObject as the base class of Data model . Generally it is believed that the performance of DependencyObject over INotifyPropertyChanged (which is lightweighted) is that DependencyObject 's impl is slightly slower.

 

 

 

TODO:study and research the performance  statistics to prove the performance advantage.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值