您可以在BorderBrush属性的绑定中使用IValueConverter吗?

25

我有一个实现了IValueConverter接口的类,旨在将布尔值转换为Brush。我正在尝试将IValueConverter用于将BorderBrush属性绑定到Border控件上:

<Window x:Class="DomPicker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespace:CustomControls;assembly=CustomControls"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        DataContext="{Binding Path=Domains, RelativeSource={RelativeSource Self}}"
        Height="350"
        Title="My Title"
        Width="525">

    <cc:CarSystemWindow.Resources>
        <cc:BooleanToBrushConverter x:Key="BrushConverter" True="Black" False="Transparent" />

        <DataTemplate x:Name="DomainTemplate" DataType="DomainViewModel">
            <Border BorderBrush="{Binding Converter=BrushConverter, Path=IsSelected}">
                . . .
            </Border>
        </DataTemplate>
    </cc:CarSystemWindow.Resources>

    <Grid>
        <ListBox . . . Name="DomainListBox" />
    </Grid>
<Window>

以下是代码后端中 Domains 属性的代码:

public static readonly DependencyProperty DomainsProperty =
    DependencyProperty.Register( "Domains", typeof( ObservableCollection<DomainViewModel> ), typeof( MainWindow ), new PropertyMetadata( null ) );

public ObservableCollection<DomainViewModel> Domains {
    get { return (ObservableCollection<DomainViewModel>) GetValue( DomainsProperty ); }
    set { SetValue( DomainsProperty, value ); }
}

当我编译代码时(这并不代表它已经完成),在BorderBrush绑定处出现编译器错误:

The TypeConverter for "IValueConverter" does not support converting from a string.

这是 IValueConverter 的代码:

public class BooleanConverter<T> : IValueConverter {

    public T False { get; set; }
    public T True { get; set; }

    public BooleanConverter( T trueValue, T falseValue ) {
        // Set the values of True and false to the values we were passed.
        True  = trueValue;
        False = falseValue;
    }

    public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) {
        return value is bool && ( (bool) value ) ? True : False;
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) {
        return value is T && EqualityComparer<T>.Default.Equals( (T) value, True );
    }
}

[ValueConversion( typeof( bool ), typeof( Brush ) )]
public class BooleanToBrushConverter : BooleanConverter<Brush> {

    /// <summary>
    /// Set us up so we convert true to a Black <see cref="SolidColorBrush"/> and
    /// false to a Red SolidColorBrush.
    /// </summary>
    public BooleanToBrushConverter() :
        base( new SolidColorBrush( Colors.Black ), new SolidColorBrush( Colors.Red ) ) { }
}

有人能告诉我我做错了什么吗?

1个回答

73
你需要使用StaticResource来访问你的转换器。
示例:
  <Border BorderBrush="{Binding IsSelected, Converter={StaticResource BrushConverter}}">

1
哇,我现在感觉好蠢啊。谢谢@sa_ddam213,你让我今天很开心! :-) - BrainSlugs83
我完全有和上面两条评论一样的感觉,而且还是同时的。 - Ajay Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接