看一段代码你就知道了:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="460" Height="200" Title="Binding the Properties of Two Controls"> <Window.Resources> <Style TargetType="TextBlock"> <Setter Property="FontSize" Value="16"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="DockPanel.Dock" Value="Top"/> <Setter Property="HorizontalAlignment" Value="Center"/> </Style> <Style TargetType="Canvas"> <Setter Property="Height" Value="50"/> <Setter Property="Width" Value="50"/> <Setter Property="Margin" Value="8"/> <Setter Property="DockPanel.Dock" Value="Top"/> </Style> <Style TargetType="ComboBox"> <Setter Property="Width" Value="150"/> <Setter Property="Margin" Value="8"/> <Setter Property="DockPanel.Dock" Value="Top"/> </Style> </Window.Resources> <Border Margin="10" BorderBrush="Silver" BorderThickness="3" Padding="8"> <DockPanel> <TextBlock>Choose a Color:</TextBlock> <ComboBox Name="myComboBox" SelectedIndex="0"> <ComboBoxItem>Green</ComboBoxItem> <ComboBoxItem>Blue</ComboBoxItem> <ComboBoxItem>Red</ComboBoxItem> </ComboBox> <Canvas> <Canvas.Background> <Binding ElementName="myComboBox" Path="SelectedItem.Content"/> </Canvas.Background> </Canvas> </DockPanel> </Border> </Window>
运行效果:
本文通过一个简单的WPF示例展示了如何将ComboBox的选择项内容绑定到Canvas背景色上,实现颜色选择的效果。代码中定义了多个样式以统一控件外观,并使用数据绑定实现了功能。
2184

被折叠的 条评论
为什么被折叠?



