User avatar
BenJo
Topic Author
Posts: 25
Joined: 20 Jun 2024, 15:42

Invert Bool Binding

11 Aug 2026, 18:48

Hey, what's the best way to invert a bool binding with Studio?
<Button IsEnabled="{Binding Path=bDisabled}"/> <!--(want !bDisabled)-->

You can use DataTriggers to set the property, or would having a converter in Studio be useful, perhaps?
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Invert Bool Binding

13 Aug 2026, 12:35

I think a converter is the best solution here. I have added BoolInvertConverter to the toolkit. It will be available in the next release candidate of 4.0.
////////////////////////////////////////////////////////////////////////////////////////////////////
bool BoolInvertConverter::TryConvert(BaseComponent* value, const Type*, BaseComponent*,
    Ptr<BaseComponent>& result)
{
    if (Boxing::CanUnbox<bool>(value))
    {
        bool currentValue = Boxing::Unbox<bool>(value);
        result.Reset(Boxing::Box(!currentValue));
        return true;
    }

    return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
bool BoolInvertConverter::TryConvertBack(BaseComponent* value, const Type*, BaseComponent*,
    Ptr<BaseComponent>& result)
{
    if (Boxing::CanUnbox<bool>(value))
    {
        bool currentValue = Boxing::Unbox<bool>(value);
        result.Reset(Boxing::Box(!currentValue));
        return true;
    }

    return false;
}
 
User avatar
BenJo
Topic Author
Posts: 25
Joined: 20 Jun 2024, 15:42

Re: Invert Bool Binding

13 Aug 2026, 13:34

Brilliant! Thanks for that, it will come in handy

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest