綁定: 與 Element Model Indexer Style RelativeSource 綁定, 以及綁定中的資料轉換
介紹
重新想象 Windows 8 Store Apps 之 綁定
與 Element 綁定
與 Model 綁定
與 Indexer 綁定
對 Style 中的 Setter 進行綁定(綁定靜態資源)
Binding 的一個擴充標記 RelativeSource 的應用
綁定中的資料轉換
樣本
1、示範如何與 Element 綁定,以及 OneTime, OneWay, TwoWay 的區別
Binding/BindingElement.xaml
<Page x:Class="XamlDemo.Binding.BindingElement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Binding" 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="120 0 0 0"> <!-- 本例用於示範如何與 Element 綁定,以及 OneTime, OneWay, TwoWay 的區別 --> <!-- OneTime 方式繫結元素 --> <Slider Name="sliderOneTime" Minimum="1" Maximum="100" Value="10" Width="180" HorizontalAlignment="Left" /> <TextBox Text="{Binding ElementName=sliderOneTime, Path=Value, Mode=OneTime}" Width="150" HorizontalAlignment="Left" /> <!-- OneWay 方式繫結元素(OneWay 是預設) --> <Slider Name="sliderOneWay" Minimum="1" Maximum="100" Value="10" Width="180" HorizontalAlignment="Left" Margin="0 50 0 0" /> <TextBox Text="{Binding ElementName=sliderOneWay, Path=Value, Mode=OneWay}" Width="150" HorizontalAlignment="Left" /> <!-- TwoWay 方式繫結元素 --> <Slider Name="sliderTwoWay" Minimum="1" Maximum="100" Value="10" Width="180" HorizontalAlignment="Left" Margin="0 50 0 0" /> <TextBox Text="{Binding ElementName=sliderTwoWay, Path=Value, Mode=TwoWay}" Width="150" HorizontalAlignment="Left" /> </StackPanel> </Grid></Page>
2、示範如何與 Model 進行雙向繫結
Binding/BindingModel.xaml
<Page x:Class="XamlDemo.Binding.BindingModel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Binding" 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="120 0 0 0" Name="root"> <TextBlock Name="lblMsg" FontSize="14.667" /> <TextBox FontSize="14.667" Text="{Binding Path=Name, Mode=TwoWay}" Margin="0 10 10 0" /> <TextBox FontSize="14.667" Text="{Binding Age, Mode=TwoWay}" Margin="0 10 10 0" /> <ToggleSwitch OffContent="女" OnContent="男" Header="性別" Margin="0 10 10 0"> <ToggleSwitch.IsOn> <Binding Path="IsMale" Mode="TwoWay" /> </ToggleSwitch.IsOn> </ToggleSwitch> </StackPanel> </Grid></Page>