Hey. I have an object that has a string property called BackgroundColor. This string is the hexidecimal representation of a color. I cannot change this object.
嘿。我有一个对象,它有一个名为BackgroundColor的字符串属性。该字符串是颜色的十六进制表示。我无法改变这个对象。
I'm binding a collection of these objects to a listView. What I would like to do is bind the background of the listview's row to the BackgroundColor property of the object that is displayed in the row.
我将这些对象的集合绑定到listView。我想要做的是将listview的行的背景绑定到行中显示的对象的BackgroundColor属性。
What is the best way to to this?
最好的方法是什么?
2 个解决方案
#1
2
I think using a IValueConverter is the appropriate solution. You could make a HexConverter that converts the string hex value to Color. That link should get you started.
我认为使用IValueConverter是合适的解决方案。您可以创建一个将字符串十六进制值转换为Color的HexConverter。该链接应该让你开始。
#2
6
You'll want to use a Style to bind the Background of ListViewItem to the item for the row. The item is the default DataContext of the ListViewItem so this should be straightforward:
您将要使用Style将ListViewItem的Background绑定到该行的项目。该项是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>
Instead of binding to the whole item you'll bind to the BackgroundColor, but it should be similar to the above. You have have to use a converter with the binding to prefix a "#", this is the signal to the built-in BrushConverter to parse it as hex.
您将绑定到BackgroundColor,而不是绑定到整个项目,但它应该类似于上面的。您必须使用带有绑定的转换器作为前缀“#”,这是内置BrushConverter将其解析为十六进制的信号。
#1
2
I think using a IValueConverter is the appropriate solution. You could make a HexConverter that converts the string hex value to Color. That link should get you started.
我认为使用IValueConverter是合适的解决方案。您可以创建一个将字符串十六进制值转换为Color的HexConverter。该链接应该让你开始。
#2
6
You'll want to use a Style to bind the Background of ListViewItem to the item for the row. The item is the default DataContext of the ListViewItem so this should be straightforward:
您将要使用Style将ListViewItem的Background绑定到该行的项目。该项是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>
Instead of binding to the whole item you'll bind to the BackgroundColor, but it should be similar to the above. You have have to use a converter with the binding to prefix a "#", this is the signal to the built-in BrushConverter to parse it as hex.
您将绑定到BackgroundColor,而不是绑定到整个项目,但它应该类似于上面的。您必须使用带有绑定的转换器作为前缀“#”,这是内置BrushConverter将其解析为十六进制的信号。