SenlyCamile
Topic Author
Posts: 16
Joined: 15 Jan 2021, 07:38

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

23 Apr 2021, 15:40

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")
};

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2255
Joined: 22 Dec 2011, 19:20

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

23 Apr 2021, 16:54

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;
}
 
KeldorKatarn
Posts: 193
Joined: 30 May 2014, 10:26

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

23 Apr 2021, 21:06

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;
    }
 
SenlyCamile
Topic Author
Posts: 16
Joined: 15 Jan 2021, 07:38

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

24 Apr 2021, 04:15

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;
}
 
User avatar
jsantos
Site Admin
Posts: 3139
Joined: 20 Jan 2012, 17:18
Contact:

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

28 Apr 2021, 11:22

Thanks for the feedback. Closing this.

Who is online

Users browsing this forum: No registered users and 2 guests