• wp7数据绑定 Carl


        Silverlight中的数据绑定对被绑定对象有特殊要求,如果只是普通的get、set属性的对象用在数据绑定上有很多问题(无法双向绑定),一般要求类实现INotifyPropertyChanged接口或者继承自DependencyObject,现在推荐用DependencyObject 的方法

    第一种绑定方式:实现INotifyPropertyChanged接口

     1 public class Person:INotifyPropertyChanged
     2     {
     3        public event PropertyChangedEventHandler PropertyChanged;
     4 
     5        private string name;
     6       public string Name
     7       {
     8          get
     9            {
    10                return name;
    11            }
    12          set
    13           {
    14                name = value;
    15               if (PropertyChanged != null)
    16                {
    17                     PropertyChanged(this, new PropertyChangedEventArgs("Name"));
    18                }
    19            }
    20        }
    21 }

    第二种绑定方式:实现DependencyObject接口(推荐)--快捷方法:输入propdp然后tab

     1  public class Person : DependencyObject
     2     {
     3         //静态的、DependencyProperty类型的、属性名+Property= DependencyProperty.Register("属性名",typeof(属性类型),
     4            // typeof(所属类),null);
     5 
     6         public static DependencyProperty NameProperty = DependencyProperty.Register("Name",typeof(string),
     7             typeof(Person),null);
     8 
     9         public string Name 
    10         {
    11             get
    12             {
    13                 return (string)GetValue(NameProperty);
    14             }
    15             set
    16             {
    17                 SetValue(NameProperty, value);
    18             }
    19         }

    三种绑定模式:

    OneTime:一次绑定,绑定创建时使用源数据更新控件。(可以是普通的set、get属性)。

    OneWay (默认值):单向绑定,在绑定创建时使用源数据更新控件,当源数据发生变化的时候也更新控件。(必须实现INotifyPropertyChanged接口或者继承自DependencyObject)。相当于Eval

    TwoWay:双向绑定,数据源的变化会更新控件,控件的变化也会更新到数据源。(必须实现INotifyPropertyChanged接口或者继承自DependencyObject)。相当于Bind

    加法计算器:

    定义一个类:

     1  public class jiafa:DependencyObject
     2     {
     3         public int sum1
     4         {
     5             get { return (int)GetValue(sum1Property); }
     6             set { SetValue(sum1Property, value); }
     7         }
     8 
     9         // Using a DependencyProperty as the backing store for sum1.  This enables animation, styling, binding, etc...
    10         public static readonly DependencyProperty sum1Property =
    11             DependencyProperty.Register("sum1", typeof(int), typeof(jiafa), null);
    12         public int sum2
    13         {
    14             get { return (int)GetValue(sum2Property); }
    15             set { SetValue(sum2Property, value); }
    16         }
    17         public static readonly DependencyProperty sum2Property = DependencyProperty.Register("sum2",typeof(int),typeof(jiafa),null);
    18  }
    19 
    20   public int result
    21         {
    22             get { return (int)GetValue(resultProperty); }
    23             set { SetValue(resultProperty, value); }
    24         }
    25 
    26         // Using a DependencyProperty as the backing store for result.  This enables animation, styling, binding, etc...
    27         public static readonly DependencyProperty resultProperty =
    28             DependencyProperty.Register("result", typeof(int), typeof(jiafa),null);

    导入命名空间

    1  xmlns:ctrl="clr-namespace:复习0819"
     1   <phone:PhoneApplicationPage.Resources>
     2         <ctrl:jiafa x:Key="jiafa"></ctrl:jiafa>
     3     </phone:PhoneApplicationPage.Resources>
     4     <!--LayoutRoot 是包含所有页面内容的根网格-->
     5   <Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{StaticResource jiafa}">
     6         <TextBox Height="72" HorizontalAlignment="Left" Margin="12,127,0,0" Name="textBox1" Text="{Binding sum1,Mode=TwoWay}" VerticalAlignment="Top" Width="460" />
     7         <TextBox Height="72" HorizontalAlignment="Left" Margin="20,271,0,0" Name="textBox2" Text="{Binding sum2}" VerticalAlignment="Top" Width="460" />
     8         <Button Content="相加" Height="72" HorizontalAlignment="Left" Margin="98,414,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click_1" />
     9         <TextBlock Height="44" HorizontalAlignment="Left" Margin="54,517,0,0" Name="textBlock1" Text="{Binding result}" VerticalAlignment="Top" Width="280" />
    10     </Grid>

    后台代码

    1  private void button1_Click_1(object sender, RoutedEventArgs e)
    2         {
    3            jiafa jf=(jiafa)this.Resources["jiafa"];
    4            jf.result = jf.sum1 + jf.sum2;
    5            
    6         }

    数据绑定的Converter:

      之前计算器的例子发现Model的属性是int类型,但是文本框的Text属性是string类型,也能双向绑定,这是因为有值转换器。

      但是当我们从数据中取出来的值是0和1,并分别要表示男和女;这个时候系统就不能帮我们进行复杂的类型转换,不可能把1变成男展示给用户;

    所以我们要自己写一个转换器

    定义一个枚举:

    1 Public enum Gender{ Female,Male,Unkown};
     1  public class GenderStringConverter:IValueConverter
     2     {
     3         //正向转换:Model到UI的转换
     4         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     5         {
     6             //value为Model中属性的值
     7             //返回值为转换后UI中的值
     8             Gender gender = (Gender)value;
     9             switch (gender)
    10             {
    11                 case Gender.Female:
    12                     return "";
    13                 case Gender.Male:
    14                     return "";
    15                 case Gender.Unkown:
    16                     return "非常男女";
    17                 default :
    18                     throw new Exception("Gender出现预期之外的值");
    19             }
    20         }
    21 
    22         //当UI变化时,把UI中的值转换为Model中的值
    23         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    24         {
    25             string s = (string)value;
    26             switch (s)
    27             {
    28                 case "":
    29                     return Gender.Male;
    30                 case "":
    31                     return Gender.Female;
    32                 case "非常男女":
    33                     return Gender.Unkown;
    34                 default :
    35                     //throw new Exception();
    36                     return Gender.Unkown;
    37             }
    38         }
    39     }

    转换器的用法:

    先在需要用到转换器的页面进行引入;

    第一步

    1 xmlns:my="clr-namespace:PhoneApp1"

    第二步

    1 <phone:PhoneApplicationPage.Resources>
    2         <my:GenderStringConverter x:Key="genderStrConverter"></my:GenderStringConverter>
    3 
    4     </phone:PhoneApplicationPage.Resources>

    第三步

    1 <TextBox Text="{Binding Gender,Mode=TwoWay,Converter={StaticResource genderStrConverter}}"></TextBox>
  • 相关阅读:
    uniapp 的 unidateformat 获取时间_data 获取格式化后的时间
    Redis_多路复用原理
    Redis_九大数据类型
    线程池注意事项
    maven 仓库优先级
    《第一行代码:Android篇》学习笔记(八)
    《第一行代码:Android篇》学习笔记(五)
    《第一行代码:Android篇》学习笔记(九)
    《第一行代码:Android篇》学习笔记(十一)
    《第一行代码:Android篇》学习笔记(三)
  • 原文地址:https://www.cnblogs.com/sc0791/p/2649808.html
Copyright © 2020-2023  润新知