如何:讓資料可於 XAML 中繫結
本主題討論您可以根據應用程式的需求,將資料提供給可延伸應用程式標記語言 (XAML) 中系結的各種方式。
範例
如果您有想要從 XAML 系結的 Common Language Runtime (CLR) 物件,讓物件可供系結的其中一種方式是將它定義為資源,並給予它 x:Key
。 在下列範例中,您有一個 Person
具有名為 PersonName
之字串屬性的 物件。 Person
物件 (在反白顯示且包含 <src>
元素的行中) 定義于名為 SDKSample
的命名空間中。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:SDKSample"
SizeToContent="WidthAndHeight"
Title="Simple Data Binding Sample">
<Window.Resources>
<src:Person x:Key="myDataSource" PersonName="Joe"/>
<Style TargetType="{x:Type Label}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="25"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="25"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Padding" Value="3"/>
</Style>
</Window.Resources>
<Border Margin="5" BorderBrush="Aqua" BorderThickness="1" Padding="8" CornerRadius="3">
<DockPanel Width="200" Height="100" Margin="35">
<Label>Enter a Name:</Label>
<TextBox>
<TextBox.Text>
<Binding Source="{StaticResource myDataSource}" Path="PersonName"
UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
</TextBox>
<Label>The name you entered:</Label>
<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>
</DockPanel>
</Border>
</Window>
然後,您可以將控制項系結 TextBlock 至 XAML 中的 物件,如包含 <TextBlock>
專案顯示的反白顯示行一樣。
或者,您可以使用 類別 ObjectDataProvider ,如下列範例所示:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:SDKSample"
xmlns:system="clr-namespace:System;assembly=mscorlib"
SizeToContent="WidthAndHeight"
Title="Simple Data Binding Sample">
<Window.Resources>
<ObjectDataProvider x:Key="myDataSource" ObjectType="{x:Type src:Person}">
<ObjectDataProvider.ConstructorParameters>
<system:String>Joe</system:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
<Style TargetType="{x:Type Label}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="25"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="25"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
</Window.Resources>
<Border Margin="25" BorderBrush="Aqua" BorderThickness="3" Padding="8">
<DockPanel Width="200" Height="100">
<Label>Enter a Name:</Label>
<TextBox>
<TextBox.Text>
<Binding Source="{StaticResource myDataSource}" Path="Name"
UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
</TextBox>
<Label>The name you entered:</Label>
<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=Name}"/>
</DockPanel>
</Border>
</Window>
您以相同的方式定義系結,就像包含 <TextBlock>
專案顯示的反白顯示行一樣。
在此特定範例中,結果相同:您有 TextBlock 具有文字內容的 Joe
。 不過,類別 ObjectDataProvider 提供功能,例如能夠系結至方法的結果。 如果您需要提供的功能,您可以選擇使用 類別 ObjectDataProvider 。
不過,如果您要系結至已建立的物件,則必須在程式碼中設定 DataContext
,如下列範例所示。
DataSet myDataSet;
private void OnInit(object sender, EventArgs e)
{
string mdbFile = Path.Combine(AppDataPath, "BookData.mdb");
string connString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile);
OleDbConnection conn = new OleDbConnection(connString);
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM BookTable;", conn);
myDataSet = new DataSet();
adapter.Fill(myDataSet, "BookTable");
// myListBox is a ListBox control.
// Set the DataContext of the ListBox to myDataSet
myListBox.DataContext = myDataSet;
}
Private myDataSet As DataSet
Private Sub OnInit(ByVal sender As Object, ByVal e As EventArgs)
Dim mdbFile As String = Path.Combine(AppDataPath, "BookData.mdb")
Dim connString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile)
Dim conn As New OleDbConnection(connString)
Dim adapter As New OleDbDataAdapter("SELECT * FROM BookTable;", conn)
myDataSet = New DataSet()
adapter.Fill(myDataSet, "BookTable")
' myListBox is a ListBox control.
' Set the DataContext of the ListBox to myDataSet
myListBox.DataContext = myDataSet
End Sub
若要使用 XmlDataProvider 類別存取 XML 資料以進行系結,請參閱 使用 XMLDataProvider 和 XPath 查詢 系結至 XML 資料。 若要使用 ObjectDataProvider 類別存取 XML 資料以進行系結,請參閱 系結至 XDocument、XElement 或 LINQ for XML 查詢結果 。
如需您可以指定系結至之資料之許多方式的相關資訊,請參閱 指定系結來源 。 如需您可以系結的資料類型,或如何實作自己的 Common Language Runtime (CLR) 物件以進行系結的詳細資訊,請參閱 系結來源概觀 。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應