简体   繁体   中英

How to use SbyteToBoolConverter converter in wpf, for converting bool property to sbyte in the UI

I have an SByteToBoolConverter which we can use with the checkbox if we had an sbyte type of property. The code goes like this:

class SbyteToBoolConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((sbyte)value == 0)
            return false;
        else
            return true;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((bool)value)
            return 1;
        else
            return 0;
    }
}

However I am looking for something exactly opposite of this, I have boolean property and I want to show 0 or 1 on the UI, probably with a combobox control. So basically I have to use an exactly opposite converter, say BoolToSbyteConverter.

My question is, can we not use the same SbyteToBoolConverter for achieving this somehow, after all we just have replace Convert and ConvertBack call.

Is there a way to work with the same converter or I have to create a new one exactly opposite of this.

Re-using this converter for the opposite direction would mean to exchange Convert and ConvertBack methods. Technically this should be possible - you would first need to extract the logic to new methods (let's for example call those ConvertBoolToSbyte and ConvertSbyteToBool ), and from Convert / ConvertBack call one of these, based on a flag that specifies the direction to be used. Such a converter could look somewhat similar to the following:

class SbyteToBoolConverter: IValueConverter
{
    public bool UseOppositeDirection { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (UseOppositeDirection)
            return ConvertSbyteToBool((sbyte)value);
        else
            return ConvertBoolToSbyte((bool)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (UseOppositeDirection)
            return ConvertBoolToSbyte((bool)value);
        else
            return ConvertSbyteToBool((sbyte)value);
    }

    public sbyte ConvertBoolToSbyte(bool input)
    {
        if (input)
            return 1;
        else
            return 0;
    }

    public bool ConvertSbyteToBool(sbyte input)
    {
        if (input == 0)
            return false;
        else
            return true;
    }
}

From XAML, you'd reference two instances of this converter, one for each direction:

<Page.Resources>
    <SbyteToBoolConverter x:Key="SbyteToBoolConverter" UseOppositeDirection="False" />
    <SbyteToBoolConverter x:Key="BoolToSbyteConverter" UseOppositeDirection="True" />
</Page.Resources>

...and use one of these within the Binding expressions, depending on the desired direction.

You might as well be able to automatically detect the desired direction based on the type of input value, and through this get rid of the UseOppositeDirection parameter:

class SbyteToBoolConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value if sbyte)
            return ConvertSbyteToBool((sbyte)value);
        else
            return ConvertBoolToSbyte((bool)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool)
            return ConvertBoolToSbyte((bool)value);
        else
            return ConvertSbyteToBool((sbyte)value);
    }

    ...
}

However: As Clemens already pointed out, creating a second converter might be much simpler, and in addition the code would be easier to read and maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM