使用Converter进行Datatemplate后台无法正常工作。 [英] Datatemplate with Converter for background not working.

查看:56
本文介绍了使用Converter进行Datatemplate后台无法正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



所以我有一个正在开发的WPF C#应用程序。

目前我正在制作一个应该在屏幕上,在左侧,显示一个对象列表。

这些对象中的每一个都有一个ResultState字段,用于确定列表中项目的背景颜色。



我创建了一个ResultStateConverter,它将值转换为Brush并将其作为资源添加到我的窗口中。



这就是我的XMAL的样子:



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:

<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

推荐答案

我发现问题不是我的XAML,而是我的IValueConveter实现。



对于遇到此问题的其他人,请检查您从转换器返回的刷子类型。



我正在返回一个System.Drawing.Brush,它需要是一个System.Windows.Media.Brush对象。
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.


这篇关于使用Converter进行Datatemplate后台无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆