我创建了一个组合框,列出了 System.Windows.Media.Colors 预定义的颜色,使用此问题中所述的方法:如何使用 XAML 在 WPF 中列出颜色? https://stackoverflow.com/questions/562682/how-can-i-list-colors-in-wpf-with-xaml
我现在的 XAML 代码是:
<Window ...>
<Window.Resources>
<ObjectDataProvider
ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="ColorList" />
<local:StringToBrushConverter x:Key="FontColorConversions" />
</Window.Resources>
<Grid Background="Black">
...
<ComboBox Grid.Column="1" Grid.Row="1" Height="22" Width="240"
VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding Source={StaticResource ColorList}}"
SelectedValue="{Binding FontColor, Mode=TwoWay}"
DisplayMemberPath="Name"
SelectedValuePath="Name">
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Foreground" Value="{Binding Converter={StaticResource FontColorConversions}}"/>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
...
</Grid>
</Window>
此外,请注意,我将 SelectedValue 绑定到 VM 类的 FontColor 属性,该属性是字符串类型。
class FontSetting : INotifyPropertyChanged
{
private string _fontColor = "Lavender"; // initial color
public event PropertyChangedEventHandler PropertyChanged;
public string FontColor
{
get
{
return _fontColor;
}
set
{
_fontColor = value;
OnPropertyChanged("FontColor");
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我将包含此 ComboBox 的窗口的 DataContext 设置为 FontSetting 的实例。
因此,组合框中的每个项目现在实际上显示一个代表某种颜色的字符串,我想要做的是将项目的前景色设置为其内容指示的颜色,如下所示:
有人可以帮忙吗?谢谢。
更新:
由于大多数解决方案都有一个将字符串转换为 Brush 的转换器,实际上我已经有了它,现在我想把我的放在这里,因为我将 TextBox 的 Foreground 绑定到 FontSetting 的 FontColor 属性,这样当您更改 ComboBox 的颜色时,该文本框会相应更改。
这是我的转换器类,现在工作正常:
class StringToBrushConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
BrushConverter conv = new BrushConverter();
SolidColorBrush brush = conv.ConvertFromString("Lavender") as SolidColorBrush;
if (null != value)
{
brush = conv.ConvertFromString(value.ToString()) as SolidColorBrush;
}
return brush;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
当我单击组合框打开下拉列表时,出现异常:
结论
阿明的解决方案有效,这是我的错误。我现在简要解释一下,如果您像我正在做的那样将 ComboBox 绑定到 System.Windows.Media.Colors,则在渲染项目时,将执行转换器类的 Convert() 方法(您将其分配给绑定),实际上,传递给 Convert() 作为其第一个参数的值是一个 Syetem.Windows.Media.Color 实例。我犯了错误,因为我认为它是字符串类型。
因此,就我而言,我需要两个转换器类,一个将字符串转换为 Brush,另一个将 Color 转换为 Brush。所以我将保留我自己的 StringToBrush 转换器并添加 Amine 的 ColorToBrush 转换器。
不过,我稍微简化了 Amine 的实现:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BrushConverter conv = new BrushConverter();
SolidColorBrush brush = SolidColorBrush)conv.ConvertFromString(FontSetting.DEFAULT_FONT_COLOR);
if (null != value)
{
PropertyInfo pi = value as PropertyInfo;
if (null != pi)
{
brush = conv.ConvertFromString(pi.Name) as SolidColorBrush;
}
}
return brush;
}
而且,Joe的意见也很有价值,把它们放在一起,我可以保持物品颜色一致,这就完美了。