Silverlight 实现INotifyPropertyChanged接口绑定数据

实例 -验证文本框单价为大于零的正数,失去焦点时执行。
Xaml:
  <Grid x:Name="LayoutRoot"Background="{x:Null}" Width="400"Height="200">
              <Grid.RowDefinitions>
                      <RowDefinition/>
                      <RowDefinition/>
                      <RowDefinition/>
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                      <ColumnDefinitionWidth="0.3*"/>
                      <ColumnDefinitionWidth="0.7*"/>
              </Grid.ColumnDefinitions>
              <sdk:Label Height="20" HorizontalAlignment="Left"Name="ProductLable" Content="ProductName:"  Width="90" />
              <sdk:Label Grid.Row="1" Height="20"HorizontalAlignment="Left" Name="PriceLable"Content="Price:"  Width="90"/>
              <TextBox Grid.Column="1" Height="23"HorizontalAlignment="Left"  Name="textBox1"Text="{Binding ProductName,Mode= OneWay}"  Width="180" />
              <TextBox Grid.Column="1" Grid.Row="1" Height="23"HorizontalAlignment="Left"  Name="textBox2"Text="{Binding Price,Mode= TwoWay,NotifyOnValidationError=True,ValidatesOnExceptions=True}"BindingValidatiWidth="180" />
              <Button Content="Change" Grid.Row="2"Grid.Column="1" Height="23"  Name="button1"Width="75" Click="button1_Click" />
      </Grid>

cs:
  public partial class DataBindingAndValidate :UserControl
      {
              Book book = new Book();

              public DataBindingAndValidate()
              {
                      InitializeComponent();
                      book.ProductName = "test product name";
                      book.Price = 12.0;
                      this.textBox1.DataContext = book;
                      this.textBox2.DataContext = book;
              }

              private void button1_Click(object sender, RoutedEventArgs e)
              {
                      book.Price = book.Price + 10.0;
              }

              private void textBox2_BindingValidationError(object sender,ValidationErrorEventArgs e)
                                 
                      if (e.Action == ValidationErrorEventActi on.Added)
                      {
                              this.textBox2.BorderBrush = new SolidColorBrush(Colors.Red);
                      }
                      else if (e.Action == ValidationErrorEventActi on.Removed)
                      {
                              this.textBox2.BorderBrush = newSolidColorBrush(Colors.LightGray);                             
                      }
              }
      }
   
  public class Book : INotifyPropertyChanged
      {
              public event PropertyChangedEventHand ler PropertyChanged;
              private void NotifyPropertyChange(string propertyName) {
                      if (PropertyChanged != null)
                      {
                              PropertyChanged(this, newPropertyChangedEventArgs (propertyName));
                                     
              }

              private string productName;

              public string ProductName
              {
                      get { return productName; }
                      set { productName = value; }
              }

              private double price;

              public double Price
              {
                      get { return price; }
                      set
                      {
                              if (value <= 0) {
                                      throw new Exception("请输入大于零的正数!");
                              }
                              price = value;
                              NotifyPropertyChange("Price");
                      }
              }
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值