Nullable Boolean Parameter in UI.

6 בJuly 2014

אין תגובות

assume you dealing with IDE for some database engine, well your filtering

:requirements is about

“give me all above 100 dollars”

“give all under 100 dollars”

and

.”give me all no matter the price”

Now representation of this functionality in UI is one Problem, we have 3 state

.checkbox, or maybe with 3 radio buttons or better will be custom 3 states control

But using on of those simple Boolean controls could made a serious head problem since it cannot be bound to Nullable Boolean and hold a three boolean fields and

,sync them it is quite a mess

And what do I mean ? indeed we can store some Enum or for DB case we will probably will store a 3 boolean field on our code, but more efficiency will be to use

.bool? Type which is built in 3 states on it

??Now back to our main question what will be on the client side

Better solution would be Converter from boolean state to nullable of Bool like this

:one with converter parameter of string or enum

 

    /// <summary>

    /// this converter takes 3 states (yes , no , null) and transfer them to bool? 

    /// </summary>

  public  class NullableBoolToRadioGroupConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            string param = parameter as string;

            if (string.IsNullOrEmpty(param))

                return DependencyProperty.UnsetValue;

 

            bool? bvalue = null;

 

            if (value != null)

            {

                if (!(value is bool?))

                    return DependencyProperty.UnsetValue;

 

                bvalue = (bool?)value;

            }

 

            bool result = false;

 

            switch (param)

            {

                case "yes":

                    {

                        result = bvalue.GetValueOrDefault();

                        break;

                    }

                case "no":

                    {

                        result = !bvalue.GetValueOrDefault(true);

                        break;

                    }

                case "all":

                    {

                        result = bvalue == null;

                        break;

                    }

                default:

                    return DependencyProperty.UnsetValue;

            }

 

            return result;

        }

 

 

Now we must use Convert back in this Binding, 

 

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            string param = parameter as string;

            if (string.IsNullOrEmpty(param))

                return DependencyProperty.UnsetValue;

 

            if (!(value is bool))

                return DependencyProperty.UnsetValue;

 

            bool isChecked = (bool)value;

 

            bool? result = null;

 

              switch (param)

              {

                  case "yes":

                      {

                          if (isChecked)

                              result = true;

                          break;

                      }

                  case "no":

                      {

                          if (isChecked)

                              result = false;

                          break;

                      }

                  case "all":

                      break;

 

                  default:

                      return DependencyProperty.UnsetValue;

              }

 

                return result;

            }


And now 3 radio buttons on the UI could ne bound to one bool? Property in your

!code

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *