程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WPF學習筆記 - 12. Binding (5)

WPF學習筆記 - 12. Binding (5)

編輯:關於.NET

10. 數據提供程序

(1) XmlDataProvider

XmlDataProvider 允許我們直接將 XML 數據作為數據源,我們將前面章節的例子改成 XML 數據島試試,注意此時我們已經不需要在代碼中定義 Personal、PersonalList 類型。

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1">
  <Window.Resources>
    <XmlDataProvider x:Key="personals" XPath="Personals">
      <x:XData>
        <Personals xmlns="">
          <Personal Name="Tom" Age="15" Sex="Male" />
          <Personal Name="Mary" Age="11" Sex="Female" />
          <Personal Name="Jack" Age="12" Sex="Male" />
        </Personals>
      </x:XData>
    </XmlDataProvider>
  </Window.Resources>
  <Grid>
    <StackPanel DataContext="{StaticResource personals}">
      <ListBox x:Name="listbox1" ItemsSource="{Binding XPath=*}">
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding XPath=@Name}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding XPath=@Age}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding XPath=@Sex}" />
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </StackPanel>
  </Grid>
</Window>

在資源中定義 XML 數據島,注意 "Personals xmlns" 不能省略,另外采用 XPath 進行了綁定操作 (XPath 的語法可參考 MSDN 文檔)。除了使用數據島,我們還以使用 XML 數據文件。

Window1.xaml

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:my="clr-namespace:Learn.WPF"
  Title="Window1">
  <Window.Resources>
    <XmlDataProvider x:Key="personals" Source="pack://siteOfOrigin:,,,/Personals.xml" 
      XPath="Personals" />

  </Window.Resources>
  <Grid>
    <StackPanel DataContext="{StaticResource personals}">
      <ListBox x:Name="listbox1" ItemsSource="{Binding XPath=*}">
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding XPath=@Name}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding XPath=@Age}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding XPath=@Sex}" />
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </StackPanel>
  </Grid>
</Window>

Personals.xml

<?xml version="1.0" encoding="utf-8" ?>
<Personals xmlns="">
  <Personal Name="Tom" Age="15" Sex="Male" />
  <Personal Name="Mary" Age="11" Sex="Female" />
  <Personal Name="Jack" Age="12" Sex="Male" />
</Personals>

在 Source 屬性中指定 XML Uri。

當然,我們也可以在程序代碼中通過 XmlDocument 來控制 XML 數據源。

Window1.xaml

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1">
  <Window.Resources>
    <XmlDataProvider x:Key="personals" />
  </Window.Resources>
  <Grid>
    <StackPanel DataContext="{StaticResource personals}">
      <ListBox x:Name="listbox1" ItemsSource="{Binding XPath=*}">
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding XPath=@Name}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding XPath=@Age}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding XPath=@Sex}" />
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </StackPanel>
  </Grid>
</Window>

Window1.xaml.cs

public partial class Window1 : Window
{
  public Window1()
  {
    InitializeComponent();
 var xml = new XmlDocument();
    xml.Load("Personals.xml");
    var provider = this.FindResource("personals") as XmlDataProvider;
    provider.Document = xml;
    provider.XPath = "Personals";
  }
}

邏輯代碼只需修改 XmlDocument 即可自動同步顯示到界面上。

protected void ButtonClick(object sender, RoutedEventArgs e)
{
  var provider = this.FindResource("personals") as XmlDataProvider;
  var xml = provider.Document;
  var mary = xml.SelectSingleNode("Personals/Personal[@Name=\"Mary\"]") as XmlElement;
  var age = Convert.ToInt32(mary.Attributes["Age"].Value);
  mary.Attributes["Age"].Value = (++age).ToString();
}

如果設置了 Source 屬性,則放棄所有內聯 XML 數據;如果設置了 Document 屬性,則清除 Source 屬性並放棄所有內聯 XML 數據。

設置以下屬性將隱式導致此 XmlDataProvider 對象刷新:Source、Document、XmlNamespaceManager 和 XPath。

在更改多個導致刷新的屬性時,建議使用 DeferRefresh。

(2) ObjectDataProvider

ObjectDataProvider 比我們直接綁定對象有如下三個好處:

可以在 XAML 申明中使用構造參數。

綁定到源對象的方法上。

支持異步數據綁定。

我們先看看構造參數的使用。

Window1.xaml.cs

enum Sex
{
  Male,
  Female
}
class Personal
{
  public string Name { get; private set; }
  public int Age { get; private set; }
  public Sex Sex { get; private set; }
  public Personal(string name, int age, Sex sex)
  {
    this.Name = name;
    this.Age = age;
    this.Sex = sex;
  }
}
public partial class Window1 : Window
{
  public Window1()
  {
    InitializeComponent();
  }
}

Window1.xaml

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:my="clr-namespace:Learn.WPF"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
  Title="Window1">
  <Window.Resources>

    <ObjectDataProvider x:Key="personal" ObjectType="{x:Type my:Personal}">
      <ObjectDataProvider.ConstructorParameters>
        <sys:String>Tom</sys:String>
        <sys:Int32>15</sys:Int32>
        <my:Sex>Male</my:Sex>
      </ObjectDataProvider.ConstructorParameters>
    </ObjectDataProvider>

  </Window.Resources>
  <Grid>
    <StackPanel DataContext="{StaticResource personal}">
      <Label Content="{Binding Name}" />
      <Label Content="{Binding Age}" />
      <Label Content="{Binding Sex}" />
    </StackPanel>
  </Grid>
</Window>

接下來,我們嘗試綁定到一個方法上。

Window1.xaml.cs

class PersonalList : ObservableCollection<Personal>
{
  public PersonalList GetPersonals()
  {
    this.Add(new Personal("Tom", 15, Sex.Male));
    this.Add(new Personal("Mary", 11, Sex.Female));
    this.Add(new Personal("Jack", 13, Sex.Male));
    return this;
  }
}
public partial class Window1 : Window
{
  public Window1()
  {
    InitializeComponent();
  }
}

Window1.xaml

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:my="clr-namespace:Learn.WPF"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
  Title="Window1">
  <Window.Resources>

    <ObjectDataProvider x:Key="personals" ObjectType="{x:Type my:PersonalList}" 
      MethodName="GetPersonals" />

  </Window.Resources>
  <Grid>
    <StackPanel DataContext="{StaticResource personals}">
      <ListBox x:Name="listbox1" ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding Path=Name}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding Path=Age}" />
              <TextBlock>,</TextBlock>
              <TextBlock Text="{Binding Path=Sex}" />
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </StackPanel>
  </Grid>
</Window>

和構造方法參數一樣,我們也可以向方法提供參數。

Window1.xaml.cs

class PersonalList : ObservableCollection<Personal>
{
  public IEnumerable<Personal> GetPersonals(int top)
  {
    this.Add(new Personal("Tom", 15, Sex.Male));
    this.Add(new Personal("Mary", 11, Sex.Female));
    this.Add(new Personal("Jack", 13, Sex.Male));
    return this.Take(top);
  }
}

Window1.xaml

<ObjectDataProvider x:Key="personals" ObjectType="{x:Type my:PersonalList}" MethodName="GetPersonals">
  <ObjectDataProvider.MethodParameters>
    <sys:Int32>2</sys:Int32>
  </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved