请原谅我的无知。我是vb.net和WPF的新手。我有一个有颜色列表的组合框,像这样。顺便说一下,这是在WPF中。
Public Sub New()
InitializeComponent()
cmbColors.ItemsSource = GetType(Colors).GetProperties()
End Sub
在XAML中,按如下方式创建组合框:
<ComboBox Name="cmbColors" HorizontalAlignment="Left" Margin="29,35,0,0"
Grid.Row="1" VerticalAlignment="Top" Width="120">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Name}" Width="16" Height="16"
Margin="0,2,5,2"/>
<TextBlock x:Name="cmbColorsText" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我想做的是,当程序运行时,开始使用灰色的背景色,当从组合框中选择新的背景色时,背景将更新。这是我试图绑定到选择的元素的XAML。
<Border BorderBrush="{x:Null}" Grid.Column="1" Grid.Row="1" Background="{Binding Text, ElementName=cmbColors}">
<TextBlock Text="PRACTICE" Style="{StaticResource linkButtons}"/>
我已经通过了边框>background>create binding>Element>comboBox的属性窗口中的每一个(字符串)类型,并且由于某种原因(我无法确定)我要么错过了适当的一个,要么正在以错误的方式看待这个。
提前感谢!!
您将Background
绑定到字符串,但背景将需要ColorBrush。所以,如果你的组合ItemsSource
已经包含了ColorBrush的项目,那么你可以只绑定SelectedItem
而不是Text
。
或者您可以在后台绑定中使用转换器,它接受字符串并返回SolidColorBrush
,例如
将绑定更改为
<Border BorderBrush="{x:Null}" Grid.Column="1" Grid.Row="1" Background="{Binding SelectedItem.Name, ElementName=cmbColors}">
<TextBlock Text="PRACTICE" Style="{StaticResource linkButtons}"/>
ComboBox的Text属性返回PropertyInfo对象的ToString()方法的结果,因此,例如,如果你选择黑色,它将是"System.Windows.Media"。