【问题标题】:Can you use an IValueConverter in a binding on the BorderBrush property?您可以在 BorderBrush 属性的绑定中使用 IValueConverter 吗?
【发布时间】:2013-08-23 02:50:13
【问题描述】:

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

<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 ) ) { }
}

谁能告诉我我做错了什么?

【问题讨论】:

    标签: wpf converter


    【解决方案1】:

    您需要使用StaticResource 访问您的转换器

    例子:

      <Border BorderBrush="{Binding IsSelected, Converter={StaticResource BrushConverter}}">
    

    【讨论】:

    • 哇哦。我现在觉得很傻。谢谢@sa_ddam213,你让我开心! :-)
    • 我感觉和上面两个cmets一模一样,同时也是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 2012-04-16
    • 2012-05-31
    • 1970-01-01
    • 2011-02-11
    • 2018-05-10
    相关资源
    最近更新 更多