若有一WPF視窗包含以下XAML,Grid包含三個控制項TextBox、Label與TextBlock
<Grid x:Name="grid1">
<TextBox Height="23" HorizontalAlignment="Left" Margin="34,26,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="34,68,0,0" Name="label2" VerticalAlignment="Top" Width="132" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="34,0,0,127" Name="textBlock1" Text="TextBlock" VerticalAlignment="Bottom" Width="125" />
</Grid>
如果為這三個控制項的屬性設定資料繫時都指定了相同的Source,那麼就代表Source要設三次,後續要更改Source時,就得改三遍。如果多個資料繫結共用了相同的Source,您可以使用DataContext來簡化設計。在WPF中每一個繼承自FrameworkElement的類別都有DataContext屬性。<TextBox Height="23" HorizontalAlignment="Left" Margin="34,26,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="34,68,0,0" Name="label2" VerticalAlignment="Top" Width="132" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="34,0,0,127" Name="textBlock1" Text="TextBlock" VerticalAlignment="Bottom" Width="125" />
</Grid>
利用Expression Blend 4,選取畫面上的Grid,從屬性視窗點選DataContext後方的「New」按鈕。
選取要繫結的自訂物件,以MyDataClass2為例:
Code:
public class MyDataClass2 : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private string _MyProperty="";
public string MyProperty {
get { return _MyProperty; }
set {
if ( _MyProperty != value ) {
_MyProperty = value;
PropertyChanged ( this , new PropertyChangedEventArgs ( "MyProperty" ) );
}
}
}
}
回到設計畫面,點選Label控制項Context屬性後方的Advanced Properties->Data Binding->選取要繫結到MyDataClass2的MyProperty。public event PropertyChangedEventHandler PropertyChanged;
private string _MyProperty="";
public string MyProperty {
get { return _MyProperty; }
set {
if ( _MyProperty != value ) {
_MyProperty = value;
PropertyChanged ( this , new PropertyChangedEventArgs ( "MyProperty" ) );
}
}
}
}
按照相同的步驟為TextBlock和TextBox控制項的Text屬性進行相同的資料繫結設定。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfDataBinding" x:Class="wpfDataBinding._7_DataContext"
Title="_7_DataContext" Height="300" Width="300">
<Grid x:Name="grid1">
<Grid.DataContext>
<local:MyDataClass2/>
</Grid.DataContext>
<TextBox Height="23" HorizontalAlignment="Left" Margin="34,26,0,0" x:Name="textBox1" VerticalAlignment="Top" Width="120" />
<Label Content="{Binding MyProperty}" Height="28" HorizontalAlignment="Left" Margin="34,68,0,0" x:Name="label2" VerticalAlignment="Top" Width="132" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="34,0,0,127" x:Name="textBlock1" Text="{Binding MyProperty}" VerticalAlignment="Bottom" Width="125" />
</Grid>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfDataBinding" x:Class="wpfDataBinding._7_DataContext"
Title="_7_DataContext" Height="300" Width="300">
<Grid x:Name="grid1">
<Grid.DataContext>
<local:MyDataClass2/>
</Grid.DataContext>
<TextBox Height="23" HorizontalAlignment="Left" Margin="34,26,0,0" x:Name="textBox1" VerticalAlignment="Top" Width="120" />
<Label Content="{Binding MyProperty}" Height="28" HorizontalAlignment="Left" Margin="34,68,0,0" x:Name="label2" VerticalAlignment="Top" Width="132" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="34,0,0,127" x:Name="textBlock1" Text="{Binding MyProperty}" VerticalAlignment="Bottom" Width="125" />
</Grid>
</Window>
沒有留言:
張貼留言