2016-04-18 49 views
3

我有一个具有资源样式的按钮。我想在按钮内容中更改TextBlock的文本。我没有找到任何解决方案。C#UWP按钮样式中的TextBlock文本更改以编程方式

有什么想法?

<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> 

+0

[ListBox项返回字符串,DataTemplate为Button时可能重复](http://stackoverflow.com/questions/34117944/listbox-items-return-string-when-datatemplate -is-button) –

+0

不重复。因为我没有使用DataTemplate,只是Content属性。 –

+0

你是什么意思的programaticaly?启用常规按钮的内容绑定?你试图完成什么? –

回答

0

你可以做一个破解,更改行:

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

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

并在代码中设置你的按钮的Tag财产。

执行此操作的正确方法是声明新的DependencyProperty从Button继承的CustomButton类型的字符串。然后将样式应用到新的CustomButton类型。将Text属性绑定到新创建的DependencyProperty

+0

谢谢:)我很愚蠢:D我用SubContent属性做了一个自定义按钮:) –

相关问题