using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WpfApplication2
{
/// <summary>
/// 继承INotifyPropertyChanged接口,当值发生改变时,向客户端发出通知。
/// </summary>
public class SliderClass : INotifyPropertyChanged
{ public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
} private double _length=0.0;
public double Length
{
get
{
return _length;
}
set
{
_length = value;
NotifyPropertyChanged("Length");
}
}
}
}
在mainpage中定义两个Slider。
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="120*"/>
<RowDefinition Height="199*"/>
</Grid.RowDefinitions>
<Slider Value="{Binding Path=Length,Mode=TwoWay}" Margin="10,0,-10,71"/>
<Slider Value="{Binding Path=Length,Mode=TwoWay}" Grid.Row="" Margin="0,47,0,123"></Slider>
</Grid>
在后台new一个SliderClass作为Grid的数据源。会发现第一个Slider会带动第二个Slider变化,反之也成立。
本文主要理解INotifyPropertyChanged接口和实现双向绑定基础以及如何调用NotifyPropertyChanged。