WPF与MVVM的实现(二)数据绑定
2017-12-23 16:26
337 查看
接触WPF已经有两年,大大小小开发过几个项目,但从来没有系统的去学习过。几次开发项目时都觉得十分的恼火,太多的事件稍微考虑不到位就会带来麻烦,为此特地系统的看了一本《C#高级编程》第10版,了解到MVVM框架,看了之后十分欢喜,本篇记录研究MVVM过程。
public class ObservableObject : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
/// <summary>
/// Raised when a property on this object has a new value.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="propertyName">The property that has a new value.</param>
public void OnPropertyChanged(string propertyName)
{
this.VerifyPropertyName(propertyName);
if (this.PropertyChanged != null)
{
var e = new PropertyChangedEventArgs(propertyName);
this.PropertyChanged(this, e);
}
}
#endregion // INotifyPropertyChanged Members
#region Debugging Aides
/// <summary>
/// Warns the developer if this object does not have
/// a public property with the specified name. This
/// method does not exist in a Release build.
/// </summary>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
string msg = "Invalid property name: " + propertyName;
if (this.ThrowOnInvalidPropertyName)
throw new Exception(msg);
else
Debug.Fail(msg);
}
}
/// <summary>
/// Returns whether an exception is thrown, or if a Debug.Fail() is used
/// when an invalid property name is passed to the VerifyPropertyName method.
/// The default value is false, but subclasses used by unit tests might
/// override this property's getter to return true.
/// </summary>
protected bool ThrowOnInvalidPropertyName { get; private set; }
#endregion // Debugging Aides
}
<Slider x:Name="slider" Value="{Binding MyValue,Mode=TwoWay}" TickFrequency="1" HorizontalAlignment="Left" Maximum="100" Minimum="0" SmallChange="1" LargeChange="10" Margin="10,187,0,0" VerticalAlignment="Top" Height="29" Width="229"/>
<TextBox x:Name="textBox" Text="{Binding MyValue,Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="20,159,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
绑定的后台数据:
private int _value = 50;
public int MyValue
{
get { return _value; }
set
{
_value = value;
OnPropertyChanged("MyValue");
}
}
完
0001 INotifyPropertyChanged接口的使用
InotifyPropertyChanged接口的作用是通知UI后台数据已经改变,UI相应的做出改变,并没有告诉具体是哪个的控件,而是先广播一样喊出xxx值改变了,谁绑定了它记得自己做改变,非常适用MVVM模式。普通的绑定后台自己修改数据是不会显示在UI上的,所以要适用该接口。public class ObservableObject : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
/// <summary>
/// Raised when a property on this object has a new value.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="propertyName">The property that has a new value.</param>
public void OnPropertyChanged(string propertyName)
{
this.VerifyPropertyName(propertyName);
if (this.PropertyChanged != null)
{
var e = new PropertyChangedEventArgs(propertyName);
this.PropertyChanged(this, e);
}
}
#endregion // INotifyPropertyChanged Members
#region Debugging Aides
/// <summary>
/// Warns the developer if this object does not have
/// a public property with the specified name. This
/// method does not exist in a Release build.
/// </summary>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
string msg = "Invalid property name: " + propertyName;
if (this.ThrowOnInvalidPropertyName)
throw new Exception(msg);
else
Debug.Fail(msg);
}
}
/// <summary>
/// Returns whether an exception is thrown, or if a Debug.Fail() is used
/// when an invalid property name is passed to the VerifyPropertyName method.
/// The default value is false, but subclasses used by unit tests might
/// override this property's getter to return true.
/// </summary>
protected bool ThrowOnInvalidPropertyName { get; private set; }
#endregion // Debugging Aides
}
0010 MVVM数据绑定
在前台建立一个Slider一个TextBox:<Slider x:Name="slider" Value="{Binding MyValue,Mode=TwoWay}" TickFrequency="1" HorizontalAlignment="Left" Maximum="100" Minimum="0" SmallChange="1" LargeChange="10" Margin="10,187,0,0" VerticalAlignment="Top" Height="29" Width="229"/>
<TextBox x:Name="textBox" Text="{Binding MyValue,Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="20,159,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
绑定的后台数据:
private int _value = 50;
public int MyValue
{
get { return _value; }
set
{
_value = value;
OnPropertyChanged("MyValue");
}
}
完
相关文章推荐
- MVVM模式和在WPF中的实现(二)数据绑定
- WPF与MVVM的实现(三)List的数据绑定
- MVVM设计模式和在WPF中的实现(四) 事件绑定
- WPF MVVM 绑定RadioButton数据
- WPF数据绑定(一):ListView + BindingList实现列表数据绑定
- wpf中利用多重绑定实现表中数据越界自动报警
- WPF and Silverlight 学习笔记(二十二):使用代码实现绑定、绑定数据的验证
- WPF入门(四)-WPF+LINQ实现数据绑定、交互及简单datagrid样式
- WPF数据绑定机制是如何实现
- MVVM设计模式和WPF中的实现(四)事件绑定
- RoboBinding:一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架
- WPF and Silverlight 学习笔记(二十五):使用CollectionView实现对绑定数据的排序、筛选、分组
- WPF and Silverlight 学习笔记(二十五):使用CollectionView实现对绑定数据的排序、筛选、分组
- 初识别mvvm+dataBinding实现数据绑定
- WPF+MVVM数据绑定问题集锦
- 豆瓣电台WP7客户端 MVVM重构记录之使用MVVM Light实现数据绑定
- MVVM数据绑定的实现方式(KVO、block、Delegate、Notification、RAC)
- WPF+MVVM数据绑定问题集锦
- js mvvm:闲来无事,实现一个只具最基本数据双向绑定的mvvm
- WPF Datagrid Header数据绑定,表头复选框实现全选、全否、部分选中,根据条目动态变化