Click here to Skip to main content
15,920,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

So I have a WPF C# application that I am developing.
Currently I am working on a screen that should, on the left, display a list of objects.
Each of these objects has a "ResultState" field that should be used to determine the background colour of the item in the list.

I have created a ResultStateConverter that turns the value into a Brush and have added this as a resource into my window.

This is what my XMAL looks like:

XML
<Window.Resources>
		<converters:ResultStateToColour x:Key="ResultStateToColourConverter" />
		<DataTemplate DataType="{x:Type metaClasses:CustomerProcessingResult}">
				<Label Content="{Binding CustomerName}" FontWeight="Bold" 
						   Background="{Binding ResultState, 
                        Converter={StaticResource ResultStateToColourConverter}}" />
		</DataTemplate>
	</Window.Resources>
	<Grid>
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="5"></ColumnDefinition>
			<ColumnDefinition></ColumnDefinition>
			<ColumnDefinition></ColumnDefinition>
			<ColumnDefinition Width="5"></ColumnDefinition>
		</Grid.ColumnDefinitions>
		<Grid.RowDefinitions>
			<RowDefinition Height="5"></RowDefinition>
			<RowDefinition></RowDefinition>
			<RowDefinition></RowDefinition>
			<RowDefinition Height="5"></RowDefinition>
		</Grid.RowDefinitions>
		
		<ListBox Grid.Column="1" Grid.Row="1" Width="150" x:Name="ListCustomerResults" >
			
			
		</ListBox>


However, when I set my item source in code behind, no colours are being set on the backgrounds.
I put a break point in my converter and it is being triggered, and the correct value is being received/returned.

Why would this not be applying the colour correctly?

Thanks
Posted

1 solution

I found out that the issue was not with my XAML, instead it was with my IValueConveter implementation.

For others that come across this issue, check what type of "Brush" you are returning from the converter.

I was returning a "System.Drawing.Brush" and it needed to be a "System.Windows.Media.Brush" object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900