【问题标题】:C# UWP TextBlock text change in Button Style programatically以编程方式更改按钮样式的 C# UWP TextBlock 文本
【发布时间】:2016-04-18 13:40:51
【问题描述】:

我有一个资源样式的按钮。我想更改按钮内容中 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>

【问题讨论】:

标签: c# xaml win-universal-app windows-10 uwp-xaml


【解决方案1】:

你可以做一个hack,换行:

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

并在代码中设置 Button 的 Tag 属性。

正确的做法是声明继承自 Button 的 CustomButton 的新 DependencyProperty 类型字符串。然后将 Style 应用到新的 CustomButton 类型。将Text属性绑定到新创建的DependencyProperty

【讨论】:

  • 谢谢 :) 我太笨了 :D 我用 SubContent 属性制作了一个自定义按钮 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2020-06-06
  • 2019-05-28
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多