C# 用于布尔转换器的WPF中的多重绑定
我正在开发一个需要检查一些可用性属性的小应用程序。我正在使用WPF作为用户界面。我需要改变一些前景颜色,如果选择从一个组合框。我有这个数据模板:C# 用于布尔转换器的WPF中的多重绑定,c#,wpf,combobox,binding,C#,Wpf,Combobox,Binding,我正在开发一个需要检查一些可用性属性的小应用程序。我正在使用WPF作为用户界面。我需要改变一些前景颜色,如果选择从一个组合框。我有这个数据模板: <DataTemplate x:Key="userTemplate"> <TextBlock VerticalAlignment="Center"> <Image Source="imgsource.png" Height="25" Width="25" /> <Run Text="{Bindi
<DataTemplate x:Key="userTemplate">
<TextBlock VerticalAlignment="Center">
<Image Source="imgsource.png" Height="25" Width="25" />
<Run Text="{Binding BooleanObjectName}" Foreground="{Binding boolobject, Converter={StaticResource convAvailability}}"/>
</TextBlock>
这有什么问题,因为在我的界面中,我总是得到黑色。有什么想法吗
任何帮助都将不胜感激。
提前感谢。正如@Funk所指出的,您退回的刷子种类不对。您应该返回一个
System.Windows.Media.Brush
对象:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BooleanObject boolobject = (BooleanObject)value;
if (boolobject.IsBoolValueOne)
return System.Windows.Media.Brushes.Green;
else if (boolobject.IsBoolValueTwo)
return System.Windows.Media.Brushes.Red;
else if (boolobject.IsBoolValueThree)
return (SolidColorBrush)(new BrushConverter().ConvertFrom("#d3d300"));
return System.Windows.Media.Brushes.Black;
}
然后,如果您对boolobject
属性的绑定实际起作用,它应该可以工作。否则您的转换器根本不会被调用
如果要绑定到对象本身,则应指定路径“.”:
<TextBlock VerticalAlignment="Center">
<Image Source="imgsource.png" Height="25" Width="25" />
<Run Text="{Binding BooleanObjectName}" Foreground="{Binding Path=., Converter={StaticResource convAvailability}}"/>
</TextBlock>
您需要从WPF项目中的System.Windows.Media
命名空间中获取一个,System.Drawing
命名空间是用于WinForms的。是否调用了转换方法?@mm8我尝试过调试,但我认为它没有得到调用called@mm8DataTemplate是在窗口外的ResourceDictionary上定义的如果绑定错误,它通常会将错误写入输出窗口,而不会中断代码,检查那里是否有任何错误信息。已解决此问题,因为我的代码未正确设置ComboBoxitem.IsSelected值。“ComboxBox项绿色选定”。除了你之外,没有其他人应该知道这意味着什么。您是否在Convert方法中设置了断点?IsBoolValueOne、IsBoolValueTwo和IsBoolValueThree属性返回什么?可能是假的。非常感谢你的帮助!
<TextBlock VerticalAlignment="Center">
<Image Source="imgsource.png" Height="25" Width="25" />
<Run Text="{Binding BooleanObjectName}" Foreground="{Binding Path=., Converter={StaticResource convAvailability}}"/>
</TextBlock>