none
WPF Data binding with convert value type RRS feed

  • Question

  • I define a field as "bit" type in tables, I'd like to binding property "Background" of Button control by condition 

    if get "TRUE" set background color as Green or not, set background color as Red


    Friday, September 18, 2009 9:42 PM

Answers

  • You need to create a class that inherits from IValueConverter which will convert a boolean to a color.

    Here is a link with an example:
    http://blogs.msdn.com/bencon/archive/2006/05/10/594886.aspx

    And here is some sample code to get you started:

    public class BooleanColorConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture ) {
            if( (bool) value ) {
                // return your desired color for True
            }
            // return your desired color for False
        }

        // .. methods snipped for brevity
    }

    Then register that class in your Resources and use it in the binding (see the link for details how).
    Friday, September 18, 2009 10:04 PM
  • The appropriate C# code would be:
    public class BrushColorConverter : IValueConverter
    {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                WpfApplication1.UserControl1 uc = new WpfApplication1.UserControl1();
                if ((bool)value)
                {
                    return uc.ColorGreen;
                }
                return uc.ColorRed;
            }       
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            } 
    }
    
    
    The converter requires a method to go from bool->color, and from Color->bool.  You are free to just not implement the latter, if you don't plan on using it, however.


    Reed Copsey, Jr. - http://reedcopsey.com
    Saturday, September 19, 2009 3:11 PM
    Moderator

All replies

  • You need to create a class that inherits from IValueConverter which will convert a boolean to a color.

    Here is a link with an example:
    http://blogs.msdn.com/bencon/archive/2006/05/10/594886.aspx

    And here is some sample code to get you started:

    public class BooleanColorConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture ) {
            if( (bool) value ) {
                // return your desired color for True
            }
            // return your desired color for False
        }

        // .. methods snipped for brevity
    }

    Then register that class in your Resources and use it in the binding (see the link for details how).
    Friday, September 18, 2009 10:04 PM
  • You'll need to implement an IValueConverter for this.  Just make a converter that converts from a bit value to green/red.
    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, September 18, 2009 10:27 PM
    Moderator
  • Thank you for information but I got this error when build project.

    '../My project path/BooleanColorConverter' does not implement interface member
    System.Windows.Data.IValueConverter.ConvertBack(object, System.Type,object, System.Globalization.CultureInfo)'

    In code.

    public partial class UserControl1 : UserControl
    {
            private System.Windows.Media.Brush _colorRed;
            private System.Windows.Media.Brush _colorGreen;   
    
            public System.Windows.Media.Brush ColorRed
            {
                get { return _colorRed; }
                set { _colorRed = value; }
            }
    
            public System.Windows.Media.Brush ColorGreen
            {
                get { return _colorGreen; }
                set { _colorGreen = value; }
            }
    
    	public UserControl1()
            {
                this.InitializeComponent();
                this.SetBrushColor();            
            }        
    
            void SetBrushColor()
            {
                BrushConverter conv = new BrushConverter();
                SolidColorBrush greenbrush = conv.ConvertFromString("#FF4DEB0F") as SolidColorBrush;
                SolidColorBrush redbrush = conv.ConvertFromString("#FFFF3200") as SolidColorBrush;
    
                ColorGreen = greenbrush;
                ColorRed = redbrush;
            } 
    }
    
    public class BrushColorConverter : IValueConverter
    {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                WpfApplication1.UserControl1 uc = new WpfApplication1.UserControl1();
                if ((bool)value)
                {
                    return uc.ColorGreen;
                }
                return uc.ColorRed;
            }        
    }

    In xaml

    <UserControl.Resources>
            <brush:BrushConverter x:Key="BrushConvert" />
    </UserControl.Resources>


    • Edited by Yoonmy Saturday, September 19, 2009 9:48 AM add xaml
    Saturday, September 19, 2009 9:09 AM
  • Thank you for information but I got this error when build project.

    '../My project path/BooleanColorConverter' does not implement interface member
    System.Windows.Data.IValueConverter.ConvertBack(object, System.Type,object, System.Globalization.CultureInfo)'


    Which happens because you don't have that method in your converter class (and thus do not implement IValueConverter correctly). Just insert that code and, if i.e. you don't need to convert back, throw a NotImplementedException or return null.
    I don't have any c#-code at hand right now, but here's a VB-bit:

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) _
    	As Object Implements System.Windows.Data.IValueConverter.ConvertBack
     Throw New NotImplementedException()
    End Function
    
    Cheers,
    Olaf
    Saturday, September 19, 2009 12:51 PM
  • The appropriate C# code would be:
    public class BrushColorConverter : IValueConverter
    {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                WpfApplication1.UserControl1 uc = new WpfApplication1.UserControl1();
                if ((bool)value)
                {
                    return uc.ColorGreen;
                }
                return uc.ColorRed;
            }       
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            } 
    }
    
    
    The converter requires a method to go from bool->color, and from Color->bool.  You are free to just not implement the latter, if you don't plan on using it, however.


    Reed Copsey, Jr. - http://reedcopsey.com
    Saturday, September 19, 2009 3:11 PM
    Moderator
  • Thank you so much, I think "ConvertBack" not important, Firstly.
    Saturday, September 19, 2009 8:32 PM
  • It is not important for you to necessarily implement it.  However, for a value converter to work, it must implement IValueConverter, which means that Convert and ConvertBack both must exist.  You can just "implement" it by having it throw, and in this case, it will work perfectly.  The code I posted will correct your issue.

    Reed Copsey, Jr. - http://reedcopsey.com
    Sunday, September 20, 2009 6:37 PM
    Moderator
  • Hi Yoonmy,

    The above community members has illustrated the issue clearly, if you are still having additional issues with this, please feel free to feed back.

    Thanks.
    Sincerely.

    Jim Zhou -MSFT
    Monday, September 21, 2009 11:46 AM