ListView Background Color
I have a ListView where one element of the Collection is a color and the element is called "ItemColor" (of type Color). I want to set a given ListViewItem to be that color.
No matter what I try, I can't get this to work. In the example below, the Background is being set to a static color, but I want this to be something like Value= Binding(ItemColor) and the background of that item to show as being that color.
Code:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=RowSelected}" Value="True">
<Setter Property="Background" Value="Gainsboro" />
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
Any help appreciated.
Re: ListView Background Color
Code:
<TextBlock Text="{Binding MyColor}" Width="80" >
<TextBlock.Background>
<SolidColorBrush Color="{Binding MyColor}" />
</TextBlock.Background>
</TextBlock>
This also doesn't work. the Background remains the default color.
Re: ListView Background Color
I got to the bottom of this and for posterity the solution is;
Assuming MyCollection has a member "Color", create an additional string member such as MyCollection.KnownColor and set it to MyCollection.Color.Name.ToString(). Then use the XML below.
Code:
<ListView VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name ="ListBoxResults"
ItemsSource="{Binding Path=MyCollection}" SelectionMode="Single">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header =" Colour " Width="60" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text=Color>
<TextBlock.Background>
<SolidColorBrush Color="{Binding KnownColor}" />
</TextBlock.Background>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header=" Size " Width="60">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding AnOther}" HorizontalAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Re: ListView Background Color
You should not bind to a color but to a brush, in your case a SolidColorBrush