我有一个资源风格的按钮。我想在Button Content中更改TextBlock的Text。我没有找到任何解决方案。
有什么想法吗?
<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="" 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 :(得分: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" />
并在代码中设置Button的Tag
属性。
执行此操作的正确方法是声明从Button继承的CustomButton的新DependencyProperty
类型字符串。然后将Style应用于新的CustomButton类型。将Text
属性绑定到新创建的DependencyProperty