更改AppBar的菜单文本

时间:2012-11-05 06:41:13

标签: c# windows-8 microsoft-metro

我有以下AppBar。

<Page.BottomAppBar>
    <AppBar x:Name="bottomAppBar" Padding="10,0,10,0">
        <Grid>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button x:Name="switchMeasurementMode" AutomationProperties.Name = "Breath rate" Style="{StaticResource AppBarButtonStyle}" Click="switchMeasurementMode_Click" />
            </StackPanel>
        </Grid>
    </AppBar>
</Page.BottomAppBar>

看起来像这样

enter image description here

我倾向于在运行时使用以下C#代码更改其文本

    private void switchMeasurementMode_Click(object sender, RoutedEventArgs e)
    {
        this.switchMeasurementMode.Name = "111";
    }

但是按钮文字没有改变。有什么我错过了吗?

2 个答案:

答案 0 :(得分:3)

如果在Windows 8 C#项目中使用AppBar的默认样式,则必须使用以下命令在XAML中更改附加属性 AutomationProperties.Name

  

AutomationProperties.Name =&#34;新名称&#34;

或使用以下代码:

  

Button.SetValue(AutomationProperties.NameProperty,&#34; new value&#34;);
  或
  AutomationProperties.SetName(Button,&#34; new value&#34;);

答案 1 :(得分:0)

该按钮是内容控件。您设置Content属性以更改内容。

 this.switchMeasurementMode.Content= "111";

Name属性是您为Button设置程序“句柄”的方式。您可以在代码编辑器中使用该名称来修改控件。在您的情况下,您正在更改名称,这意味着您无法说出..switchMeasurementMode ......

仅供参考,内容不仅仅是文字。大多数XAML元素都可以作为内容添加。