Silverlight 实现INotifyPropertyChanged接口绑定数据

Silverlight 实现INotifyPropertyChanged接口绑定数据,第1张

概述实例 -验证文本框单价为大于零的正数,失去焦点时执行。 Xaml:   <Grid x:Name="LayoutRoot"Background="{x:Null}" Width="400"Height="200">               <Grid.RowDefinitions>                       <RowDefinition/>                   实例 -验证文本框单价为大于零的正数,失去焦点时执行。
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");                       }               }       } 总结

以上是内存溢出为你收集整理的Silverlight 实现INotifyPropertyChanged接口绑定数据全部内容,希望文章能够帮你解决Silverlight 实现INotifyPropertyChanged接口绑定数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1009928.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存