2017-06-16 62 views
0

我正在开发一个需要检查某些可用性属性的小应用程序。我正在使用WPF的用户界面。如果从组合框中进行选择,我需要更改一些前景色。我有这样的DataTemplate:布尔转换器在WPF中的多个绑定

<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> 

所以我用这个皈依一个的IValueConverter那台色到前景:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    BooleanObject boolobject = (BooleanObject)value; 
    if (boolobject.IsBoolValueOne) return System.Drawing.Brushes.Green; 
    else if (boolobject.IsBoolValueTwo) return System.Drawing.Brushes.Red; 
    else if (boolobject.IsBoolValueThree) return (SolidColorBrush)(new BrushConverter().ConvertFrom("#d3d300")); 
    else return System.Drawing.Brushes.Black; 
} 

什么是错,因为在我界面我总是得到黑色。对此有何想法?

任何帮助将非常感激。 在此先感谢。

+1

你需要一个[刷子](https://msdn.microsoft.com/en-us /library/system.windows.media.brushes(v=vs.110).aspx)从WPF项目中的System.Windows.Media命名空间中获取,System.Drawing命名空间用于WinForms。 – Funk

+0

你的Convert方法被调用了吗? – mm8

+0

@ mm8我试过调试,我认为它没有被调用 – R3muSGFX

回答

1

正如@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> 
+0

没关系。解决了这个问题,因为我的代码没有正确设置ComboBoxitem.IsSelected值。 – R3muSGFX

+0

“combobox item green selected”。除了你以外,没有其他人能够知道这意味着什么。你在Convert方法中放置了一个断点吗? IsBoolValueOne,IsBoolValueTwo和IsBoolValueThree属性返回什么?可能是错误的。 – mm8

+0

非常感谢你的帮助! – R3muSGFX