string sample="sample1234";WPF Window的XAML之中包含一個TextBlock
<TextBlock Height="23" Text="{Binding }" HorizontalAlignment="Left" Margin="221,43,0,0" Name="textBlock1" VerticalAlignment="Top" Width="95" />只要設定Text為{Binding},然後在Window_Loaded事件加上這行
textBlock1.DataContext = sample;執行就會自動將變數sample的內容,繫結到TextBlock的Text屬性。
若是使用TextBox,可以在Window中宣告屬性
private string _hi; public string HI { get { return "HI"; } set { _hi = value; } }XAML
<TextBox Height="23" Text="{Binding Path=HI,Mode=TwoWay}" HorizontalAlignment="Left" Margin="210,113,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />Window_Loaded事件加上這行
textBox1.DataContext = this;
沒有留言:
張貼留言