3

I have a button with Resource style. I would like to change Text of TextBlock in Button Content. I didn't find any solution.

Any idea?

<Style x:Key="NavigationLogoutButtonStyle" TargetType="Button" BasedOn="{StaticResource NavigationBackButtonNormalStyle}">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Height" Value="48"/>
<Setter Property="Width" Value="NaN"/>
<Setter Property="MinWidth" Value="48"/>
<Setter Property="AutomationProperties.Name" Value="Logout"/>
<Setter Property="Content">
    <Setter.Value>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="48" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <FontIcon Grid.Column="0" FontSize="16" Glyph="&#xE1E0;" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            <StackPanel Grid.Column="1" Orientation="Vertical">
                <TextBlock Style="{ThemeResource BodyTextBlockStyle}" Text="!!!TEXT HERE PROGRAMATICALLY!!!" Foreground="{StaticResource MainColorBrush}" FontSize="13" VerticalAlignment="Center" />
                <TextBlock Style="{ThemeResource BodyTextBlockStyle}" Text="{StaticResource LogoutButtonText}" VerticalAlignment="Center" />
            </StackPanel>
        </Grid>
    </Setter.Value>
</Setter>

  • Possible duplicate of [ListBox items return string, when DataTemplate is Button](http://stackoverflow.com/questions/34117944/listbox-items-return-string-when-datatemplate-is-button) – Salah Akbari Apr 18 '16 at 13:44
  • Not duplicate. Because I am not using DataTemplate, just Content property. – Dávid Imre Apr 18 '16 at 13:52
  • What do you mean by programaticaly? Enable the content Binding of a regular button? what is it you are trying to achieve exactly? – Petr Vávro Apr 18 '16 at 14:06

1 Answers1

0

You can do a hack, change the line:

<TextBlock Style="{ThemeResource BodyTextBlockStyle}" Text="!!!TEXT HERE PROGRAMATICALLY!!!" Foreground="{StaticResource MainColorBrush}" FontSize="13" VerticalAlignment="Center" />

to

<TextBlock Style="{ThemeResource BodyTextBlockStyle}" Text="{Binding Tag}" Foreground="{StaticResource MainColorBrush}" FontSize="13" VerticalAlignment="Center" />

And set the Tag property of your Button in code.

The proper way to do it would be declare new DependencyProperty type string of a CustomButton which inherits from Button. Then apply the Style to the new CustomButton type. Bind the Text property to the newly created DependencyProperty

thang2410199
  • 1,932
  • 2
  • 17
  • 18