6

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

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

但随后显示按钮文本内的标准图标。

你知道如何以正确的方式去做吗?提前感谢您的帮助 Maciek

4

1 回答 1

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

}
于 2012-11-07T20:33:12.313 回答