C# wpf 中的資料繫結4-2(15)
阿新 • • 發佈:2018-12-17
- 同一個資料來源繫結到兩個或多個控制元件上。如我們的示例中把ListBox的選中項繫結到TextBox與TextBlock。
- 在繫結語法中增加一個 Mode 屬性,即繫結模式。對於我們的示例,我們把TextBlock的繫結語法中的Mode屬性設為 OneWay 。把TextBox的繫結語法中的Mode屬性設為TwoWay。
<StackPanel Grid.Row="1"> <TextBlock Width="248" Height="24" Text="顏色:" TextWrapping="Wrap"/> <ListBox x:Name="listColor" Width="248" Height="56"> <ListBoxItem Content="Blue"/> <ListBoxItem Content="Red"/> <ListBoxItem Content="Green"/> <ListBoxItem Content="Gray"/> <ListBoxItem Content="Cyan"/> <ListBoxItem Content="GreenYellow"/> <ListBoxItem Content="Orange"/> </ListBox> <TextBlock Width="248" Height="24" Text="改變背景色:" /> <TextBlock Width="248" Height="24" Text="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}" Background="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}"> </TextBlock> <TextBox Name="txtTwoWay" Text="{Binding ElementName=listColor,Path=SelectedItem.Content,Mode=TwoWay}" Background="{Binding ElementName=listColor,Path=SelectedItem.Content,Mode=TwoWay}"></TextBox> </StackPanel>