Page 1 of 1

I try to convert dependencyproperty to bool,but it does not work. Any bugs?

Posted: 23 Apr 2021, 15:40
by SenlyCamile
Xaml
<ResourceDictionary>
  <BoolConverter x:Key="BoolConverter"/>
</ResourceDictionary>

<StackPanel>
  <RadioButton IsChecked="{Binding CameraSpeedRatio,Converter={StaticResource BoolConverter},ConverterParameter=1,Mode=TwoWay}"/>
  <RadioButton IsChecked="{Binding CameraSpeedRatio,Converter={StaticResource BoolConverter},ConverterParameter=1.5,Mode=TwoWay}"/>
  <RadioButton IsChecked="{Binding CameraSpeedRatio,Converter={StaticResource BoolConverter},ConverterParameter=2,Mode=TwoWay}"/>
  <RadioButton IsChecked="{Binding CameraSpeedRatio,Converter={StaticResource BoolConverter},ConverterParameter=3,Mode=TwoWay}"/>
</StackPanel>
Cpp
static const Noesis::DependencyProperty* CameraSpeedRatio_;

data->RegisterProperty<double>(CameraSpeedRatio_, "CameraSpeedRatio",
  Noesis::FrameworkPropertyMetadata::Create(1, Noesis::FrameworkPropertyMetadataOptions_None));

class EnumToBoolConverter : public BaseValueConverter
{
public:
  bool TryConvert(BaseComponent* value, const Type* targetType, BaseComponent* parameter,
    Noesis::Ptr<BaseComponent>& result)
  { return value->Equals(parameter); }

  bool TryConvertBack(BaseComponent* value, const Type* targetType, BaseComponent* parameter,
    Noesis::Ptr<BaseComponent>& result)
  {
    if (Boxing::CanUnbox<bool>(value) && Boxing::Unbox<bool>(value) == true)
    { result = *parameter; return true; }
    return false;
  }

private:
  NS_IMPLEMENT_INLINE_REFLECTION_(BoolConverter, BaseValueConverter, "BoolConverter")
};

Re: I try to convert dependencyproperty to bool,but it does not work. Any bugs?

Posted: 23 Apr 2021, 16:54
by sfernandez
The code in the converter is not correct. First you are comparing the CommandParameter which is a string, with the value of CameraSpeedRatio which is probably a float (I will assume that in my code). Then you are not storing the converted value in the result variable. The code should look like this:
bool TryConvert(BaseComponent* value, const Type* targetType, BaseComponent* parameter, Ptr<BaseComponent>& result)
{
    float v1 = Boxing::Unbox<float>(value);
    float v2 = StringToFloat(Boxing::Unbox<String>(parameter));
    result = Boxing::Box(v1 == v2);
    return true;
}

bool TryConvertBack(BaseComponent* value, const Type* targetType, BaseComponent* parameter, Ptr<BaseComponent>& result)
{
    bool isParamValue = Boxing::Unbox<bool>(value);
    float v1 = 0.0f; // some default value
    float v2 = StringToFloat(Boxing::Unbox<String>(parameter));
    result = Boxing::Box(isParamValue ? v2 : v1));
    return true;
}

Re: I try to convert dependencyproperty to bool,but it does not work. Any bugs?

Posted: 23 Apr 2021, 21:06
by KeldorKatarn
The ConvertBack method for this kind of converter used to bind against radio buttons should probably return Binding.DoNothing for the false case so only the RadioButton matching the value is activated and sets its value.

In C# this type of converter usually works like this:
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value?.Equals(parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value?.Equals(true) == true ? parameter : Binding.DoNothing;
    }

Re: I try to convert dependencyproperty to bool,but it does not work. Any bugs?

Posted: 24 Apr 2021, 04:15
by SenlyCamile
It works.Thanks.
The code in the converter is not correct. First you are comparing the CommandParameter which is a string, with the value of CameraSpeedRatio which is probably a float (I will assume that in my code). Then you are not storing the converted value in the result variable. The code should look like this:
bool TryConvert(BaseComponent* value, const Type* targetType, BaseComponent* parameter, Ptr<BaseComponent>& result)
{
    float v1 = Boxing::Unbox<float>(value);
    float v2 = StringToFloat(Boxing::Unbox<String>(parameter));
    result = Boxing::Box(v1 == v2);
    return true;
}

bool TryConvertBack(BaseComponent* value, const Type* targetType, BaseComponent* parameter, Ptr<BaseComponent>& result)
{
    bool isParamValue = Boxing::Unbox<bool>(value);
    float v1 = 0.0f; // some default value
    float v2 = StringToFloat(Boxing::Unbox<String>(parameter));
    result = Boxing::Box(isParamValue ? v2 : v1));
    return true;
}

Re: I try to convert dependencyproperty to bool,but it does not work. Any bugs?

Posted: 28 Apr 2021, 11:22
by jsantos
Thanks for the feedback. Closing this.