Question

How can I bind a value of string Y or N to a isEnabled Value?

<TextBox IsEnabled="{Binding Path=StringValueFromSomeEntity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

StringValueFromSomeEntity can be a Y or N value.

Was it helpful?

Solution

Use a converter to convert the string to a bool-value:

public class StringToBoolConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.ToString().ToLower() == "y")
           return true;
        return false;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value)
           return "Y";
        return "N";
    }
}

Refer to it in the resources:

<Window.Resources>
    <conv:StringToBoolConverter x:Key="StringToBool"/>
</Window.Resources>

Apply it to your binding (if you just want to change the IsEnabled property according to your string, use Mode=OneWay, but if you really want to bind TwoWay you need the ConvertBack-method):

<TextBox IsEnabled="{Binding Path=StringValueFromSomeEntity, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ResourceKey=StringToBool}"/>

OTHER TIPS

You can create an IValueConverter subclass like this:

public class YNBoolConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (value as string) == 'Y';
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value ? 'Y' : 'N';
    }
}

You need ConvertBack if you are expecting to bind TwoWay. Then add it to the resources of your page. And add it to the binding

{Binding Path=StringValueFromSomeEntity, Mode=TwoWay, 
 UpdateSourceTrigger=PropertyChanged, Converter={StaticResource YNBoolConverter}}

By default string cann't be converted into Boolen type so you have to tell WPF how to convert and take the value in place where you want to have.

here are two ways to implement this.

Using ValueConverter (prefered way)

Add a Converter into your project like below.

   public class StringToBoolConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string val = System.Convert.ToString(value).ToUpper();
                if (string.IsNullOrWhiteSpace(val))
                    return false;

                return val == "Y" ? true : false;

            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

Add the namespace into you window and add resource

<Window.Resources>
        <convrter:StringToBoolConverter x:Key="stringtoboolConverter"/>
    </Window.Resources>

Now refrance this convert into IsEnabled Propery of Checkbox.

  <GroupBox Header="With Converter" >
                <StackPanel>
                    <TextBox x:Name="txt1" Text="Y"  />
                    <CheckBox  IsEnabled="{Binding ElementName=txt1,Path=Text,Converter={StaticResource stringtoboolConverter}}" />

                </StackPanel>
            </GroupBox>

Using Style / Triggers (alternative way)

<TextBox x:Name="txt" Text="Y"  />
                <CheckBox  Content="IsEnabled" Tag="{Binding ElementName=txt,Path=Text}" >
                    <CheckBox.Style>
                        <Style TargetType="{x:Type CheckBox}">
                            <Style.Triggers>
                                <Trigger  Property="Tag" Value="Y"  >
                                    <Setter Property="IsEnabled"  Value="true"/>
                                </Trigger>
                                <Trigger  Property="Tag" Value="N"  >
                                    <Setter Property="IsEnabled"  Value="false"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </CheckBox.Style>
                </CheckBox>

Use a ValueConverter.

Create a class which implements IValueConverter

for example

public class StringToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string value = (string)value;
        return value.Trim().ToLower() == "y";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();

    }
}

After that you can it applay to your binding

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top