Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a class that implements IValueConverter that is intended to be used to convert a Boolean value into a Brush. I'm trying to use the IValueConverter in a binding to the BorderBrush property of a Border Control:

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

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 BorderBrush binding:

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

Here is the code for the 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?

like image 441
Tony Vitabile Avatar asked Aug 20 '13 21:08

Tony Vitabile


People also ask

What is IValueConverter?

The IValueConverter interface consists of two methods, Convert() and ConvertBack() . Convert method gets called when source updates target object. ConvertBack method gets called when target updates source object.

What is IValueConverter in wpf?

ValueConverters.rar. A Value Converter functions as a bridge between a target and a source and it is necessary when a target is bound with one source, for instance you have a text box and a button control. You want to enable or disable the button control when the text of the text box is filled or null.


1 Answers

You need to use StaticResource to access your converter

Example:

  <Border BorderBrush="{Binding IsSelected, Converter={StaticResource BrushConverter}}"> 
like image 84
sa_ddam213 Avatar answered Oct 25 '22 10:10

sa_ddam213