I'm facing a problem with ToolbarItem
and IsEnabled
property when trying to turn it on/off from XAML using triggers. ToolbarItem
doesn't support triggers, so what I do is to create a Button
(a hidden one) which supports triggers and then bind Button.IsEnabled
to ToolbarItem.IsEnabled
; here is the sample code:
<ContentPage.ToolbarItems>
<ToolbarItem x:Name="tlbSave" Text="Save" Clicked="Handle_Clicked">
<ToolbarItem.IsEnabled>
<Binding Source="{x:Reference btnTest}" Path="IsEnabled" />
</ToolbarItem.IsEnabled>
</ToolbarItem>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout Padding="10" VerticalOptions="CenterAndExpand">
<Entry x:Name="txtTest" HorizontalOptions="FillAndExpand" />
<Button x:Name="btnTest" Text="HIDDEN" IsEnabled="false" HorizontalOptions="FillAndExpand">
<Button.Triggers>
<MultiTrigger TargetType="Button">
<MultiTrigger.Conditions>
<BindingCondition Binding="{Binding Source={x:Reference txtTest}, Path=Text.Length,
Converter={convert:IsPositiveIntegerConverter}}" Value="true" />
</MultiTrigger.Conditions>
<Setter Property="IsEnabled" Value="True" />
</MultiTrigger>
</Button.Triggers>
</Button>
</StackLayout>
</ContentPage.Content>
If you try this piece of code you will see how btnTest
gets enable/disable when txtTest.Text
has some value. But it isn't affecting tlbSave.IsEnabled
property.
However, this work perfect in code behind when I set tlbSave.IsEnabled
into btnText.PropertyChanged
EventHandler
btnTest.IsVisible
is false, I'm just showing it up for testing purposes.
Any idea about how to deal with this?