简体   繁体   English

WPF 组合框项目背景绑定到项目属性

[英]WPF Combobox Item Background Binding to Item Property

How can I get the background colour of the Combobox Items to be bound to the boolean Value property of the model?如何获取要绑定到模型的布尔值属性的组合框项的背景颜色?

After searching and trying I can figure this out but I'm not sure this is the right approach.在搜索和尝试之后,我可以弄清楚这一点,但我不确定这是正确的方法。 It seems rather long-winded好像比较啰嗦

* NOTE: in the process of drafting this question I have arrived at the below solution *注意:在起草这个问题的过程中,我得出了以下解决方案

I have a simple code example of what I'm working with :我有一个简单的代码示例,说明我正在使用的内容:

I have a Model and ViewModel like:我有一个模型和视图模型,如:

    public class YesNoModel
    {
        public string Name { get; set; }
        public bool Value { get; set; }
    }

    public class ViewModel
    {
        public IEnumerable<YesNoModel> YesNoItems { get; set; }

        public ViewModel()
        {
            var list = new List<YesNoModel>();

            list.Add(new YesNoModel() { Name = "Yes", Value = true });
            list.Add(new YesNoModel() { Name = "No", Value = false });

            YesNoItems = list;
        }
    }

A Value Converter to convert boolean to colour将布尔值转换为颜色的值转换器

    public class BoolToBackgroundGrayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((bool)value)
            {
                return Brushes.LightGray;
            }
            else
            {
                return Brushes.Transparent;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

And XAML binding to a Combobox in a Window和 XAML 绑定到窗口中的组合框

    <Window.Resources>
        <local:BoolToBackgroundGrayConverter x:Key="BoolToBackgroundGrayConverter"/>
    </Window.Resources>
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Grid>
        <ComboBox ItemsSource="{Binding YesNoItems}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" Background="{Binding Value, Converter={StaticResource BoolToBackgroundGrayConverter}}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>            
        </ComboBox>
    </Grid>

Is this the correct approach?这是正确的方法吗? It seems rather long-winded & also it feels somehow wrong to be hardcoding the colours in the Value Converter这似乎相当冗长,而且在值转换器中对颜色进行硬编码也感觉有些错误

An alternative without a Binding Converter could be a DataTrigger:没有绑定转换器的替代方案可能是 DataTrigger:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Name}">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Background" Value="Transparent"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Value}" Value="True">
                            <Setter Property="Background" Value="LightGray"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </DataTemplate>
</ComboBox.ItemTemplate> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM