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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18345452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:28:26  来源:igfitidea点击:

Can you use an IValueConverter in a binding on the BorderBrush property?

wpfconverter

提问by Tony Vitabile

I have a class that implements IValueConverterthat is intended to be used to convert a Boolean value into a Brush. I'm trying to use the IValueConverterin a binding to the BorderBrushproperty of a BorderControl:

我有一个实现类,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>

Here is the code for the Domains property in the code behind:

以下是后面代码中 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 ); }
}

When I compile the code, which is by no means finished, I get a compiler error on the BorderBrushbinding:

当我编译代码时,这绝不是完成,我在BorderBrush绑定上收到编译器错误:

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

Here is the code for the IValueConverter:

这是代码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 ) ) { }
}

Can anyone tell me what I'm doing wrong?

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

回答by sa_ddam213

You need to use StaticResourceto access your converter

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

Example:

例子:

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