简体   繁体   English

WPF Listview:如何在 CodeBehind 中更改 ListViewItem 的背景?

[英]WPF Listview: How to change background of an ListViewItem in CodeBehind?

I have a WPF ListView with a simple data binding:我有一个带有简单数据绑定的 WPF ListView:

<ListView Name="MeasurementListView">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn x:Name="MeasurementListView_Name" Width="100" DisplayMemberBinding="{Binding Name}" />
                            <GridViewColumn x:Name="MeasurementListView_Color" Width="100" DisplayMemberBinding="{Binding Color}" />
                        </GridView>
                    </ListView.View>
                </ListView>

I bind it, as soon as it is loaded from the database and the user click the button load:我绑定它,只要它从数据库中加载并且用户单击按钮加载:

    private void FillMeasurementListView()
    {
        MeasurementListView.ItemsSource = this.myData;
    }

Now the Color column is a RGB information (eg 200, 200, 200) which I can cast into a System.Drawing.Color easily.现在 Color 列是一个 RGB 信息(例如 200、200、200),我可以轻松地将其转换为 System.Drawing.Color。 So at the moment the text is displayed (200, 200, 200) but instead I want to hide the text and color the background of this ListViewItem in the given RGB color.所以目前显示文本(200、200、200),但我想隐藏文本并用给定的 RGB 颜色为这个 ListViewItem 的背景着色。

So without a databinding (Im new to WPF and MVVM though) I would look for an entry point where the data is loaded (eg a loaded-event), find my cell / column and empty the displyed text and set the background color, Sadly I can't find such and event in this ListViewControle.所以没有数据绑定(我是 WPF 和 MVVM 的新手)我会寻找加载数据的入口点(例如加载事件),找到我的单元格/列并清空显示的文本并设置背景颜色,可悲的是我在这个 ListViewControle 中找不到这样的事件。

Any ideas how to solve this?任何想法如何解决这个问题?

You need to set the style for Item:您需要为 Item 设置样式:

<ListView Name="MeasurementListView">
    <ListView.View>
        <GridView>
            <GridViewColumn x:Name="MeasurementListView_Name" Width="100" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn x:Name="MeasurementListView_Color" Width="100" DisplayMemberBinding="{Binding Color}" />
        </GridView>
    </ListView.View>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="Background" Value="{Binding Color, Converter=ColorToBrushConverter}"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

ColorToBrushConverter - IValueConverter to convert your type's Color property to a Brush type (SolidColorBrush). ColorToBrushConverter - IValueConverter 将您的类型的 Color 属性转换为 Brush 类型 (SolidColorBrush)。


Or without a Converter, if the Color property is of type System.Windows.Media.Color .:或者没有转换器,如果Color属性是System.Windows.Media.Color类型:

<Style TargetType="ListViewItem">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="{Binding Color}"/>
        </Setter.Value>
    </Setter>
</Style>

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

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