Since it is possible to add a Binding to a BindableProperty via DataBinding, should also be able to remove Binding from BindableProperty via DataTrigger?
<Style TargetType="Label" x:Key="MyStyle">
<Style.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference mySwitch}, Path=IsToggled}" Value="True">
<Setter Property="Text">
<Setter.Value>
<Binding Path="Name" />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference mySwitch}, Path=IsToggled}" Value="False">
<Setter Property="Text">
<Setter.Value>
<!--The Binding removal should occur here-->
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
Of course, I'm talking about removing from the Style level in XAML, not from the code level. From the code level, we can do BindableObject.RemoveBinding, but how to do it in XAML?
You can use Path="null"
but I checked when it loads first time IsToggled=True
I can see Text being set, when I change i.e IsToggled=False
the switch the text is blank. That's what is expected according to your question.
However when I toggle switch again IsToggled=True
I don't see the text with the binding Name
however.
It doesn't seem there is XAML that is exactly equivalent to BindableObject.RemoveBinding
I think good bet is to rely on C# code for the same.
Update:
One more trigger when IsToggled=false
is not required at all. Just remove it. The first trigger activates and binds only when IsToggled=True
when its false Binding
is itself removed.
<ResourceDictionary>
<Style TargetType="Label"
x:Key="MyStyle">
<Style.Triggers>
<DataTrigger TargetType="Label"
Binding="{Binding Source={x:Reference mySwitch}, Path=IsToggled,Mode=TwoWay}"
Value="True">
<Setter Property="Text">
<Setter.Value>
<Binding Path="FirstName" />
</Setter.Value>
</Setter>
</DataTrigger>
<!-- end of all triggers -->
</Style.Triggers>
<!-- end of style -->
</Style>
</ResourceDictionary>
I tested this and it works. Hope this is what you are looking for.