日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

MVVM 数据绑定

發(fā)布時間:2023/11/27 生活经验 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MVVM 数据绑定 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、在 XAML 中創(chuàng)建綁定

  1. 定義源對象。

    C#??
    public class Dog
    {public string DogName { get; set; }
    }
    ??
    ?
  2. 在 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">
  3. Resources 節(jié)創(chuàng)建源對象的實例。

    XAML??
    <Grid.Resources><my:Dog x:Name="MyDog" DogName="Spot"/>
    </Grid.Resources>
  4. 通過設(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)建綁定

  1. 添加 System.Windows.Data 命名空間。

    C#??
    using System.Windows.Data; (Binding類的命名空間)
    ??
    ?
  2. 定義源對象。

    C#??
    public class Dog
    {public string DogName { get; set; }
    }
    ??
    ?
  3. 創(chuàng)建要綁定到的 FrameworkElement。

    XAML??
    <Grid x:Name="LayoutRoot" Background="White"><TextBlock x:Name="MyTextBlock" Text="Test"/>
    </Grid>
  4. 創(chuàng)建源對象的實例。

    C#??
    Dog MyDog = new Dog();
    MyDog.DogName = "Spot";
    ??
    ?
  5. 創(chuàng)建綁定對象。

    C#??
    Binding MyBinding = new Binding();
    ??
    ?
  6. 對綁定對象設(shè)置綁定屬性。

    C#??
    MyBinding.Path = new PropertyPath("DogName");
    MyBinding.Mode = BindingMode.OneTime;
    ??
    ?
  7. 通過設(shè)置 Source 屬性或 DataContext 屬性來設(shè)置綁定源。該元素的所有子級都繼承 DataContext。

    C#??
    MyBinding.Source = MyDog;
    ??
    ?

    - 或 -

    C#??
    MyTextBlock.DataContext = MyDog;
    ??
    ?
  8. 將此綁定附加到 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é)

以上是生活随笔為你收集整理的MVVM 数据绑定的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。