如何从代码隐藏更改样式为AppBarButtonStyle的Button的内容?我想改变一个图标,我这样做:
private void ChangemyButtonIcon()
{
myButton.SetValue(AutomationProperties.NameProperty, "new text");
myButton.Content = ""; // <--- works wrong
}
但不是按钮文本显示中的标准图标。
你知道如何正确地做到这一点吗? 提前感谢您的帮助 Maciek
答案 0 :(得分:4)
你可以这样做
<Style x:Key="MusicOffButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="AutomationProperties.AutomationId" Value="MusicOffButton"/>
<Setter Property="AutomationProperties.Name" Value="Music OFF"/>
<Setter Property="Content" Value=""/>
</Style>
<Style x:Key="MusicOnButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="AutomationProperties.AutomationId" Value="MusicOnButton"/>
<Setter Property="AutomationProperties.Name" Value="Music ON"/>
<Setter Property="Content" Value=""/>
</Style>
private void SetMusicButtonIcon()
{
MusicButton.Style = (ifMuted) ? (this.Resources["MusicOffButtonStyle"] as Style) : (this.Resources["MusicOnButtonStyle"] as Style);
}