本文系統闡述 WPF 資料繫結核心機制:繫結目標與來源、四種繫結模式、更新觸發時機、XML/物件繫結、DataTemplate 視覺化、CollectionView 排序分組、DataContext 範圍繼承。含豐富 XAML 範例與圖解,適合 WPF 開發者全面掌握資料繫結。
必要條件:
DependencyProperty 的可存取屬性 (如 TextBox.Text, TextBlock.Text, Background)兩大輔助提供者:
XmlDataProvider:繫結 XML 文件/片段ObjectDataProvider:繫結 CLR 物件/方法結果範例:TextBlock.Text 繫結 ListBox.SelectedItem.Content
<TextBlock Width="248" Height="24">
<TextBlock.Text>
<Binding ElementName="lbColor" Path="SelectedItem.Content" />
</TextBlock.Text>
</TextBlock>
<TextBlock Width="248" Height="24"
Text="{Binding ElementName=lbColor, Path=SelectedItem.Content}" />

關鍵屬性:ElementName (來源控制項名稱)、Path (來源屬性路徑)
| 模式 | 資料流向 | 觸發更新 | 典型用途 | 預設適用目標 |
|---|---|---|---|---|
| OneWay | 來源 → 目標 | 來源變更時 | 唯讀顯示、標籤、進度條 | TextBlock.Text, ProgressBar.Value |
| TwoWay | 雙向同步 | 雙向變更時 | 表單編輯、輸入控制項 | TextBox.Text, CheckBox.IsChecked, Slider.Value |
| OneTime | 來源 → 目標 (僅一次) | 啟動或 DataContext 變更時 | 靜態資料、快照比較、來源未實作 INotifyPropertyChanged | 無預設,需顯式指定 |
| OneWayToSource | 目標 → 來源 | 目標變更時 | 唯寫回報、來源不主動推送 | 無預設,需顯式指定 |
<!-- 唯讀顯示:OneWay -->
<TextBlock Text="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=OneWay}"
Background="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=OneWay}" />
<!-- 可編輯雙向:TwoWay -->
<TextBox Text="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=TwoWay}"
Background="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=OneWay}" />


選型建議:
| 值 | 觸發時機 | 適用場景 | 預設控制項 |
|---|---|---|---|
| LostFocus | 控制項失去焦點 | 標準表單輸入,避免頻繁更新 | TextBox (預設) |
| PropertyChanged | 屬性每次變更 | 即時搜尋、字數統計、滑桿 | Slider, TextBox (需顯式指定) |
| Explicit | 程式碼呼叫 BindingExpression.UpdateSource() | 驗證後統一提交、多欄位原子更新 | 無預設 |
<TextBox Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
內嵌 XML 範例:
<StackPanel.Resources>
<XmlDataProvider x:Key="MoreColors" XPath="/colors">
<x:XData>
<colors>
<color name="pink"/>
<color name="white"/>
<color name="black"/>
<color name="cyan"/>
<color name="gray"/>
<color name="magenta"/>
</colors>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>

關鍵屬性:
x:Key:資源鍵值,供繫結參考XPath:指定資料根節點 (如 /colors)Source:外部 XML 檔案路徑 (替代內嵌)ListBox 繫結語法:
<ListBox x:Name="lbColor"
ItemsSource="{Binding Source={StaticResource MoreColors}, XPath=color/@name}"
IsSynchronizedWithCurrentItem="True" />
Source:指定資源 (StaticResource 不追蹤變更 / DynamicResource 追蹤)XPath:節點選擇路徑 (color/@name 取 name 屬性)外部 XML 檔案:
<XmlDataProvider x:Key="Colors" Source="Colors.xml" XPath="/colors" />
<StackPanel.Resources>
<ObjectDataProvider x:Key="persons"
ObjectType="{x:Type svc:PersonService}"
MethodName="GetPersonList" />
</StackPanel.Resources>
ObjectType:提供資料的型別 (需匯入 CLR 命名空間 xmlns:svc="clr-namespace:DataBindingWPF")MethodName:呼叫的方法 (需為靜態或無參建構子)IsAsynchronous="True":非同步載入,UI 不凍結ConstructorParameters / MethodParameters / ObjectInstance:進階參數控制<DataTemplate x:Key="personLayout" DataType="{x:Type svc:Person}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=FullName}" FontWeight="Bold" Foreground="Blue" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Title}" />
<TextBlock Text=", " />
<TextBlock Text="{Binding Path=City}" />
</StackPanel>
</StackPanel>
</DataTemplate>
<ListBox x:Name="lbPersons"
ItemsSource="{Binding Source={StaticResource persons}}"
ItemTemplate="{DynamicResource personLayout}"
IsSynchronizedWithCurrentItem="True" />


核心優勢:資料邏輯與視覺呈現完全分離,DataTemplate 可重用、可替換、可動態切換。
<CollectionViewSource x:Key="personView"
Source="{Binding Source={StaticResource persons}}">
<CollectionViewSource.SortDescriptions>
<ComponentModel:SortDescription PropertyName="City" Direction="Ascending" />
<ComponentModel:SortDescription PropertyName="FullName" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"GroupDescriptions (分組)、Filter (篩選邏輯)問題:多控制項繫結同一來源,重複 Source={StaticResource ...}
解決:在父容器設定 DataContext,子元素自動繼承:
<!-- 繁瑣寫法 -->
<StackPanel>
<TextBlock Text="{Binding Source={StaticResource personView}, Path=FullName}" />
<TextBlock Text="{Binding Source={StaticResource personView}, Path=Title}" />
<TextBlock Text="{Binding Source={StaticResource personView}, Path=City}" />
</StackPanel>
<!-- 優雅寫法 -->
<StackPanel DataContext="{Binding Source={StaticResource personView}}">
<TextBlock Text="{Binding Path=FullName}" />
<TextBlock Text="{Binding Path=Title}" />
<TextBlock Text="{Binding Path=City}" />
</StackPanel>
繼承規則:若容器無 DataContext,向外層巢狀查找,直到根元素或 Application 層級。
{Binding ...} 簡潔易讀xmlns:local="clr-namespace:..."WPF 資料繫結靈活強大,掌握核心機制即能構建可維護、高效能的資料驅動 UI。