WP8.1 Study5:Data binding数据绑定

一、数据绑定

  最简单的编程UI控件的方法是写自己的数据来获取和设置控件的属性,e.g. , textBox1.Text = "Hello, world"; 但在复杂的应用程序,这样的代码很快就会变得笨拙,容易出错

因此,为了更加方便,使用XAML数据绑定到你的UI链接到一个在应用程序中包含应用程序的数据的类。

  • 这个类class:是一个对于被称为视图模型的数据绑定(ViewModel)的数据源。
  • UI控件可以从视图模型Viewmodel类的属性自动获取其显示值,而且通过改变Viewmodel属性可以更新控件显示。

  • 用户的输入可以自动更新视图模型ViewModel类的绑定属性。

1、在xaml中数据绑定Data binding in XAML

如例子:

<TextBlock x:Name="DirectionsTextBlock" TextWrapping="Wrap" 
           Margin="12,0,0,0" Text="{Binding Directions}" />

在上面的例子中,TextBlock的Text属性绑定到一些数据源的特性。那么我们可以怎样要怎样定义数据源呢?下面是两种方法:

  • Datacontext:任何FrameworkElement派生类的DataContext属性(可以直接在控件设置,但通常是设置在一个容器类型的控件,如Grid,或者直接设置在Page上)
  • ItemSource:一些列表控件的ItemSource属性。

2、数据绑定模式Data binding modes

如例子:

 <TextBlock x:Name="DirectionsTextBlock" TextWrapping="Wrap" 
            Margin="12,0,0,0" Text="{Binding Directions, Mode=OneWay}" />

模式属性决定改变如何在目标控件和数据源之间的同步

  • OneTime--控件属性设置后,数据值之后的任何后续变化都会被忽略
  • OneWay--数据对象的变化是可以同步到控件属性,但反之不行
  • TwoWay --变化的数据对象是可以同步的控件属性,反之也可以。

3、INotifyPropertyChanged

对象被设置成OneWay或者Twoway绑定必须实现INotifyPropertyChanged接口。

我们可以在ViewModel类中写入如下代码:

public class ItemViewModel : INotifyPropertyChanged
{
    private string _id;
    /// Sample ViewModel property; 
    public string ID
    {
        get { return _id; }
        set {
            if (value != _id) {
                _id = value;
                NotifyPropertyChanged("ID"); }
        }     }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

这种方法还有更好的方法,有兴趣的可以去查阅相关资料。

4、数据绑定到列表Binding to Lists

 <ListView x:Name="IngredientsLIstBox"            ItemTemplate="{StaticResource MyDataTemplate}"            DataContext="{StaticResource RecipeViewModel}"/>            ItemsSource="{Binding Ingredients}"/>

在这个例子中,DataContext是RecipeViewModel对象,而ItemsSource绑定到名为Ingredients的对象,它里面是对象的集合。但是,如果你希望当子项目被添加或删除而你的UI能自动更新时,数据源应该是一个ObservableCollection<T>(实现了INotifyCollectionChanged),而且在集合中的项目需要实现INotifyPropertyChanged。就如同:

public class RecipeDetails : INotifyPropertyChanged
 { 
     /// <summary>
     /// A collection for ItemViewModel objects.
     /// </summary>
     public ObservableCollection<ItemViewModel> Items { get; private set; }

     public void LoadData()
     {
       this.Items.Add(new ItemViewModel() { ID = "0", LineOne = "runtime one", LineTwo = ... });
       this.Items.Add(new ItemViewModel() { ID = "1", LineOne = "runtime two", LineTwo = ... });
       this.Items.Add(new ItemViewModel() { ID = "2", LineOne = "runtime three", LineTwo =...});
     }
     
     ...}

5、数据绑定提高:FallBackValue and TargetNullValue

TargetNullValue允许你当指定数据源的属性返回空时,指定替代属性或值时显示的属性。

<Button Content="{Binding Path=NextItem, Mode=OneWay, 
        TargetNullValue={Binding Path=NullValue}}" />

FallbackValue允许当你被绑定到属性不存在指定替代属性或值显示。

<TextBlock Text="{Binding Path=badPath, 
           FallbackValue='this is a fallback value'}"     
           Grid.Column="1"> </TextBlock>
原文地址:https://www.cnblogs.com/NEIL-X/p/4147445.html