大家好!这是交易...
我在窗口中定义了一个 ComboBox。此组合框的内容只是一个字符串列表,其中包含 Brushes 类中的所有值。到目前为止没有什么特别的......
但我想要实现的是一种在用户更改颜色时更改组合框背景颜色的方法。颜色将是列表中选择的值。
我正在阅读触发器和 setter ,但我仍然不知道该怎么做。
(顺便说一句,我已经有一个转换器可以在有效的画笔中转换字符串。)
<ComboBox Name="cmbColor" >
<ComboBox.Triggers >
<Trigger Property="SelectedIndex" > // <- Pseudocode!
<Setter Property = "Foreground" Value="select_value_in_combo,Converter={StaticResource ColorConverter}"/>
</Trigger>
</ComboBox.Triggers>
</ComboBox>
想法?
谢谢!
请您参考如下方法:
您可以简单地使用数据绑定(bind)来解决这个问题:
<Window x:Class="ComboBoxBackgroundSpike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ComboBoxBackgroundSpike"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.Resources>
<local:StringToBrushConverter x:Key="StringToBrush"/>
</Grid.Resources>
<ComboBox Background="{Binding RelativeSource={RelativeSource Self},
Path=SelectedValue,
Converter={StaticResource StringToBrush}}"
ItemsSource="{Binding}">
</ComboBox>
</Grid>
</Window>
