AppBarButton样式:从代码更改内容

时间:2012-11-01 14:00:11

标签: c#

如何从代码隐藏更改样式为AppBarButtonStyle的Button的内容?我想改变一个图标,我这样做:

private void ChangemyButtonIcon()
{
   myButton.SetValue(AutomationProperties.NameProperty, "new text");
   myButton.Content = "&#xE15D;"; // <--- works wrong
}

但不是按钮文本显示中的标准图标。

你知道如何正确地做到这一点吗? 提前感谢您的帮助 Maciek

1 个答案:

答案 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);

}