案例地址:GitHub - microsoft/WPF-Samples: Repository for WPF related samples
一、运行界面
二、案例功能描述
Canvas的背景色绑定下拉框的值
三、分析代码
<ComboBox Name="myComboBox" SelectedIndex="0">
<ComboBoxItem>Green</ComboBoxItem>
<ComboBoxItem>Blue</ComboBoxItem>
<ComboBoxItem>Red</ComboBoxItem>
</ComboBox>
<Canvas Height="75" Margin="213,8,196,8" Width="Auto"<Canvas Height="75" Margin="213,8,196,8" Width="Auto" Background="{Binding ElementName=myComboBox, Path=SelectedItem.Content}">
</Canvas>
因为没有设置DataContext,所以要指定ElementName