SL4.データバインディング--OneWay、OneTime、TwoWay

9774 ワード

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を実現することに重点を置いています.