Binding to a Windows Store AppBar button text

Is it possible to bind to the AppBarButtonStyle Caption?

In this tutorial you will learn how to bind to the AppBarButtonStyle Caption in a Windows Store App. You might need this if you have a list of buttons with the same icon but variable text.

vera

Default behavior

When you decorate a Windows Store App Button with a style from StandardStyles.xaml, your buttons will look like this:

buttons

A typical button style from StandardStyles.xaml has this XAML code:

<Style x:Key="FolderppBarButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource AppBarButtonStyle}">
    <Setter Property="AutomationProperties.AutomationId" Value="FolderAppBarButton"/>
    <Setter Property="AutomationProperties.Name" Value="Folder"/>
    <Setter Property="Content" Value="&#xE188;"/>
</Style>

The Button Text AutomationProperties.Name is hardcoded in the style. First I tried to bind this value using {Binding Name} but this did not work.

Solution

The trick is to put the binding on the Button instance as an attached property like this:

<Button AutomationProperties.Name="{Binding}" Style="{StaticResource FolderppBarButtonStyle}" />
Written by Loek van den Ouweland on February 29, 2016. Questions regarding this artice? You can send them to the address below.
By using this site, you acknowledge that you have read and understand our Cookie and Privacy Policy.