-
- SenlyCamile
- Posts: 20
- Joined:
I try to convert dependencyproperty to bool,but it does not work. Any bugs?
Xaml
Cpp
Code: Select all
<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>
Code: Select all
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")
};-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: I try to convert dependencyproperty to bool,but it does not work. Any bugs?
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:
Code: Select all
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: 243
- Joined:
Re: I try to convert dependencyproperty to bool,but it does not work. Any bugs?
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:
In C# this type of converter usually works like this:
Code: Select all
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
- Posts: 20
- Joined:
Re: I try to convert dependencyproperty to bool,but it does not work. Any bugs?
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:Code: Select allbool 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?
Thanks for the feedback. Closing this.
Who is online
Users browsing this forum: No registered users and 7 guests