背水一戰 Windows 10 之 綁定: 與 Element 綁定, 與 Indexer 綁定, TargetNullValue - 當綁定數據為 null 時顯示的值, FallbackValue - 當綁定失敗時顯示的值 ...
背水一戰 Windows 10 (18) - 綁定: 與 Element 綁定, 與 Indexer 綁定, TargetNullValue, FallbackValue
作者:webabcd
介紹
背水一戰 Windows 10 之 綁定
- 與 Element 綁定
- 與 Indexer 綁定
- TargetNullValue - 當綁定數據為 null 時顯示的值
- FallbackValue - 當綁定失敗時顯示的值
示例
1、演示如何與 Element 綁定
Bind/BindingElement.xaml
<Page x:Class="Windows10.Bind.BindingElement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Bind" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <!-- 本例用於演示如何與 Element 綁定,以及 OneTime, OneWay, TwoWay 的區別 --> <!-- OneTime 方式綁定元素 --> <TextBox Text="{Binding ElementName=txtOneTime, Path=Text, Mode=OneTime}" /> <TextBox Name="txtOneTime" Text="OneTime" Margin="0 10 0 0" /> <!-- OneWay 方式綁定元素(OneWay 是預設方式) --> <TextBox Text="{Binding ElementName=txtOneWay, Path=Text, Mode=OneWay}" Margin="0 30 0 0" /> <TextBox Name="txtOneWay" Text="OneWay" Margin="0 10 0 0" /> <!-- TwoWay 方式綁定元素(同時演示一下 Binding 標記的另一種寫法,就是直接把 Path 指定的路徑放到 Binding 的後面) --> <TextBox Text="{Binding Text, ElementName=txtTwoWay, Mode=TwoWay}" Margin="0 30 0 0" /> <TextBox Name="txtTwoWay" Text="TwoWay" Margin="0 10 0 0" /> <!-- TwoWay 方式綁定元素(在 C# 端指定 Binding 對象) --> <TextBox Name="textBox1" Margin="0 30 0 0" /> <TextBox Name="textBox2" Text="TwoWay" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
Bind/BindingElement.xaml.cs
/* * 演示如何與 Element 綁定 */ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Data; namespace Windows10.Bind { public sealed partial class BindingElement : Page { public BindingElement() { this.InitializeComponent(); this.Loaded += BindingElement_Loaded; } // 在 C# 端做綁定 private void BindingElement_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // 實例化 Binding 對象 Binding binding = new Binding() { ElementName = nameof(textBox2), Path = new PropertyPath(nameof(TextBox.Text)), Mode = BindingMode.TwoWay // 預設是 OneWay 的 }; // 將目標對象的目標屬性與指定的 Binding 對象關聯 BindingOperations.SetBinding(textBox1, TextBox.TextProperty, binding); } } }
2、演示如何與 Indexer 綁定
Bind/BindingIndexer.xaml
<Page x:Class="Windows10.Bind.BindingIndexer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Bind" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <!--演示如何綁定集合中的某個元素--> <TextBlock Name="textBlock" Text="{Binding Path=[3]}" /> <!--演示如何綁定集合中的某個對象的某個屬性--> <TextBlock Name="textBlock2" Text="{Binding Path=[5].Name}" Margin="0 10 0 0" /> <!--演示如何綁定 string 類型的索引器--> <TextBlock Name="textBlock3" Text="{Binding Path=[webabcd]}" Margin="0 10 0 0" /> <!--演示如何綁定字典表中指定 key 的數據--> <TextBlock Name="textBlock4" Text="{Binding Path=[hello]}" Margin="0 10 0 0" /> <!--演示如何在 C# 端綁定索引器--> <TextBox Name="textBox" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
Bind/BindingIndexer.xaml.cs
/* * 演示如何與 Indexer 綁定 */ using System; using System.Collections.Generic; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Data; using Windows10.Common; namespace Windows10.Bind { public sealed partial class BindingIndexer : Page { public BindingIndexer() { this.InitializeComponent(); this.Loaded += BindingIndexer_Loaded; BindingDemo(); } private void BindingIndexer_Loaded(object sender, RoutedEventArgs e) { // 用於演示如何綁定集合中的某個元素 List<string> list = new List<string>(); for (int i = 0; i < 10; i++) { list.Add("索引:" + i.ToString()); } textBlock.DataContext = list; // 用於演示如何綁定集合中的某個對象的某個屬性 textBlock2.DataContext = TestData.GetEmployees(); // 用於演示如何綁定 string 類型的索引器 textBlock3.DataContext = this; // 用於演示如何綁定字典表中指定 key 的數據 Dictionary<string, string> dic = new Dictionary<string, string>() { { "hello", "hello webabcd" } }; textBlock4.DataContext = dic; } // 自定義一個索引器 public object this[string indexer] { get { return "string: " + indexer; } } // 在 C# 端綁定索引器 private void BindingDemo() { textBox.DataContext = this; // 實例化 Binding 對象 Binding binding = new Binding() { Path = new PropertyPath("[wanglei]") }; // 將目標對象的目標屬性與指定的 Binding 對象關聯 BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding); /* * 註:經測試在 TextBox 做如上綁定是正常的。但是如果在 TextBlock 做如上綁定則運行時報錯 Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 不知道為什麼 */ } } }
3、演示 Binding 中的 TargetNullValue 和 FallbackValue 的用法
Bind/TargetNullValueFallbackValue.xaml
<Page x:Class="Windows10.Bind.TargetNullValueFallbackValue" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Bind" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <!-- FallbackValue - 當綁定失敗時顯示的值 --> <TextBlock Name="textBlock1" Text="{Binding Path=MyName, FallbackValue='綁定失敗時的預設值'}" Margin="5" /> <!-- TargetNullValue - 當綁定數據為 null 時顯示的值 --> <TextBlock Name="textBlock2" Text="{Binding Path=MyName, TargetNullValue='綁定數據的返回值為 null'}" Margin="5" /> </StackPanel> </Grid> </Page>
Bind/TargetNullValueFallbackValue.xaml.cs
/* * 演示 Binding 中的 TargetNullValue 和 FallbackValue 的用法 */ using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.Bind { public sealed partial class TargetNullValueFallbackValue : Page { public TargetNullValueFallbackValue() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 為 textBlock2 提供數據上下文 textBlock2.DataContext = this; /* // 實例化 Binding 對象 Binding binding = new Binding() { Path = new PropertyPath("Name"), TargetNullValue = "TargetNullValue", FallbackValue = "FallbackValue" }; // 將目標對象的目標屬性與指定的 Binding 對象關聯 BindingOperations.SetBinding(textBlock2, TextBox.TextProperty, binding); */ } public string MyName { get; set; } = null; } }
OK
[源碼下載]