C# wpf 中的数据绑定4-2(15)

版权声明:随便看,喜欢的话加我qq,一起讨论。 https://blog.csdn.net/qq_43687284/article/details/85047952
  1. 同一个数据源绑定到两个或多个控件上。如我们的示例中把ListBox的选中项绑定到TextBox与TextBlock。
  2. 在绑定语法中增加一个 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>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43687284/article/details/85047952