gpt4 book ai didi

WPF:将listviewitem的背景颜色绑定(bind)到对象的十六进制字符串属性

转载 作者:行者123 更新时间:2023-12-04 18:00:08 26 4
gpt4 key购买 nike

嘿。我有一个对象,它有一个名为 BackgroundColor 的字符串属性。此字符串是颜色的十六进制表示。我无法更改此对象。

我将这些对象的集合绑定(bind)到 listView。我想做的是将 ListView 行的背景绑定(bind)到行中显示的对象的 BackgroundColor 属性。

最好的方法是什么?

最佳答案

您需要使用 Style 将 ListViewItem 的背景绑定(bind)到该行的项目。该项目是 ListViewItem 的默认 DataContext,所以这应该很简单:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.Resources>
<x:Array x:Key="colors" Type="{x:Type sys:String}">
<sys:String>Red</sys:String>
<sys:String>Yellow</sys:String>
<sys:String>#0000FF</sys:String>
</x:Array>
</Grid.Resources>
<ListView ItemsSource="{StaticResource colors}">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="{Binding .}"/>
</Style>
</ListView.Resources>
</ListView>
</Grid>

您将绑定(bind)到 BackgroundColor,而不是绑定(bind)到整个项目,但它应该与上述类似。您必须使用带有绑定(bind)前缀的转换器“#”,这是内置 BrushConverter 将其解析为十六进制的信号。

关于WPF:将listviewitem的背景颜色绑定(bind)到对象的十六进制字符串属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/360369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com