SL4.数据绑定OneWay、OneTime、TwoWay

XAML:

           <Button Name="MyButton" Content="Update"  Click="MyButton_Click"  Width="65" Height="41"></Button>
<TextBlock Height="23" Name="tbtitle" Text="{Binding Title,Mode=OneTime}" />
<TextBlock Height="23" Name="tbtitles" Text="{Binding Title,Mode=OneWay}" />
<TextBlock Height="23" Name="tbprice" Text="{Binding Price,Mode=TwoWay}" />

  

XAML.CS:

       public partial class MainPage : UserControl
{
Book book
= new Book();
public MainPage()
{
InitializeComponent();
book.Title
= "CCTV-5频道";
book.Price
= 12;
tbtitles.DataContext
= book;
tbtitle.DataContext
= book;
tbprice.DataContext
= book;
}

private void MyButton_Click(object sender, RoutedEventArgs e)
{
book.Price
= 44;
book.Title
= "中华电台-5频道";
}

  

Bool.CS:
 

    public class Book : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _Title;
public string Title
{
get { return _Title; }
set
{
_Title
=value;
if (PropertyChanged != null)
{
PropertyChanged(
this, new PropertyChangedEventArgs("Title"));
}
}
}
private decimal _price;
public decimal Price
{
get
{
return _price;
}
set
{
_price
= value;
if (PropertyChanged != null)
{
PropertyChanged(
this, new PropertyChangedEventArgs("Price"));
}
}
}
}

  

代码很简单,重点在于实体类要实现INotifyPropertyChanged


原文地址:https://www.cnblogs.com/baobao2010/p/2152067.html