Is there a simple solution to show yes and no in combobox silverlight and bind to a database?

I have a bit defined in my database 0 = no, 1 = yes. I have a combination of silverlight with the values ​​Yes and No. How can I bind my bit value to combos?

+3
source share
1 answer

You do not specify which data access machine you are using, but typical tools will expose a bit as a logical property. The easiest approach is to use a value converter.

Here is the basic idea (more secure coding may be required): -

public class BoolToStringConverter : IValueConverter
{
    public String FalseString { get; set; }
    public String TrueString { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return FalseString;
        else
            return (bool)value ? TrueString : FalseString;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(TrueString);
    }
}

Resources ( App.xaml)

<Resources>
   <local:BoolToStringConverter x:Key="CvtYesNo" FalseString="No" TrueString="Yes" />
</Resources>

combobox : -

<ComboBox SelectedItem="{Binding YourBitField, Converter={StaticResource CvtYesNo}, Mode=TwoWay}">
   <sys:String>Yes<sys:String>
   <sys:String>No<sys:String>
</ComboBox>
+5

Source: https://habr.com/ru/post/1734463/


All Articles