MVVM 数据绑定
一、在 XAML 中創(chuàng)建綁定
-
定義源對象。
C#? ? public class Dog {public string DogName { get; set; } }
? ? ?
-
在 XAML 中創(chuàng)建對源對象的命名空間的引用。
XAML? ? <UserControl x:Class="BindingXAML.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:BindingXAML">
-
在 Resources 節(jié)創(chuàng)建源對象的實例。
XAML? ? <Grid.Resources><my:Dog x:Name="MyDog" DogName="Spot"/> </Grid.Resources>
-
通過設(shè)置 Source 屬性或 DataContext 屬性綁定到源對象。該元素的所有子級都繼承 DataContext。
XAML? ? <TextBlock Text="{Binding DogName, Source={StaticResource MyDog}, Mode=OneTime}"/>- 或 -
XAML? ? <TextBlock Text="{Binding DogName, Mode=OneTime}" DataContext="{StaticResource MyDog}"/>
此例中數(shù)據(jù)源是一個XAML對象,該對象是代碼中創(chuàng)建的MyDog類的一個實例。Binding是在XAML中的TextBlock中指定的Binding,如示例中的代碼那樣指定的話,也就相當(dāng)于創(chuàng)建了以個Binding對象 并制定其路徑(DogName)和模式等。
?
二、使用代碼創(chuàng)建綁定
-
添加 System.Windows.Data 命名空間。
C#? ? using System.Windows.Data; (Binding類的命名空間)
? ? ? -
定義源對象。
C#? ? public class Dog {public string DogName { get; set; } }? ? ? -
創(chuàng)建要綁定到的 FrameworkElement。
XAML? ? <Grid x:Name="LayoutRoot" Background="White"><TextBlock x:Name="MyTextBlock" Text="Test"/> </Grid>
-
創(chuàng)建源對象的實例。
C#? ? Dog MyDog = new Dog(); MyDog.DogName = "Spot";
? ? ? -
創(chuàng)建綁定對象。
C#? ? Binding MyBinding = new Binding();
? ? ? -
對綁定對象設(shè)置綁定屬性。
C#? ? MyBinding.Path = new PropertyPath("DogName"); MyBinding.Mode = BindingMode.OneTime;? ? ? -
通過設(shè)置 Source 屬性或 DataContext 屬性來設(shè)置綁定源。該元素的所有子級都繼承 DataContext。
C#? ? MyBinding.Source = MyDog;
? ? ?
- 或 -
C#? ? MyTextBlock.DataContext = MyDog;
? ? ? -
將此綁定附加到 FrameworkElement 的屬性。
C#? ? MyTextBlock.SetBinding(TextBlock.TextProperty, MyBinding);
以上示例中是通過代碼創(chuàng)建綁定的示例。這個示例中在TextBlock的Text屬性中就沒有創(chuàng)建Binding對象了,而是通過后臺創(chuàng)建好Binding對象,并設(shè)置好屬性,然后通過MyTextBlock.SetBinding(TextBlock.TextProperty, MyBinding);將TEXT屬性進行綁定。
?
在兩個示例中都可以看到,指定數(shù)據(jù)源有兩種方法,以TextBlock為例,可以指定Binding類的Source屬性到數(shù)據(jù)源,也可以將TextBlock的DataContext指定到數(shù)據(jù)源,效果一樣,如果TextBlock沒有指定數(shù)據(jù)源,會在其綁定的Binding中尋找是否有數(shù)據(jù)源。
?
?
在實際開發(fā)中,為了讓程序更可看更有條理,我們會混合使用兩種方法,及在后臺創(chuàng)建數(shù)據(jù)源,在XAML中綁定。這樣前臺就免去了創(chuàng)建數(shù)據(jù)源的XAML元素,后臺省去了創(chuàng)建Binding類的代碼,并且通過查看前臺的XAML代碼就可以很容易的判斷出各個控件的綁定數(shù)據(jù)。
?
代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
?
?
?
namespace SilverlightTest
{
??? public partial class Databind : UserControl
??? {
??????? public Databind()
??????? {
???????????
???? ???????InitializeComponent();
??????? }
?
??????? private void UserControl_Loaded(object sender, RoutedEventArgs e)
??????? {
??????????? Mind m = new Mind();
??????????? txt1.DataContext = m;
?
??????? }
?
??? }
?
??? public class Mind? //數(shù)據(jù)源
??? {
??????? string _info="OK";
?
??????? public string Info
??????? {
??????????? get
??????????? {
??????????????? return _info;
??????????? }
??????????? set
??????????? {
??????????????? _info = value;
??????????? }
??????? }
??? }
}
?
XAML:
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" ?x:Class="SilverlightTest.Databind"
?? ?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
?? ?xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
?? ?Width="400" Height="300" Loaded="UserControl_Loaded">
??? <Grid x:Name="LayoutRoot" Background="White">
???????
?????????? <TextBlock ?x:Name="txt1" Text="{Binding Info,Mode=OneTime}" />
?
??? </Grid>
</UserControl>
?
這樣的寫法讓數(shù)據(jù)綁定可讀性更高,也更容易理解,最常用的方式。
?
下面來比對下XAML創(chuàng)建和代碼創(chuàng)建:
?
{Binding Info,Mode=OneTime}?
相當(dāng)于
Binding MyBinding = new Binding();?
MyBinding.Path = new PropertyPath("Info"); MyBinding.Mode = BindingMode.OneTime;
?
Text="{Binding Info,Mode=OneTime}"
相當(dāng)于
MyTextBlock.SetBinding(TextBlock.TextProperty, MyBinding);
?
?
<TextBlock Text="{Binding DogName, Mode=OneTime}" DataContext="{StaticResource MyDog}"/>中的 DataContext="{StaticResource MyDog}
相當(dāng)于
txt1.DataContext = m;
??
以上就是在Silverlight中數(shù)據(jù)綁定的一些簡要介紹。
混合XAML 和代碼的創(chuàng)建數(shù)據(jù)綁定的方式是最容易理解和閱讀的,所以我們經(jīng)常會看到在使用DataGrid的通過制定ItemsSource 到數(shù)據(jù)源后,在XAML中就直接使用"{Binding Address}"進行數(shù)據(jù)綁定了,DataGrid中的ItemSource也就相當(dāng)于TextBlock的DataContext屬性了,其他控件的數(shù)據(jù)綁定方法了類似,另外數(shù)據(jù)源可以用類,也可以是字符串類型等常量類型,使用常量類型時,就不需要指定Binding的Path屬性了,因為他本身就是數(shù)據(jù)。
原文:http://blog.csdn.net/banmuhuangci/article/details/4160092
轉(zhuǎn)載于:https://www.cnblogs.com/Yu-weiz/archive/2013/01/06/2847181.html
總結(jié)
- 上一篇: 削骨瘦脸多少钱
- 下一篇: 《你不可不知的人性》讲述了什么?书籍《你