WPF:如何修改标签?

时间:2014-04-17 14:34:06

标签: wpf wpf-controls

在我的MainWindow.xaml中,我有:

<Window x:Class="UnionPayExhangeRate3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Union Pay Exchange Rate" Height="350" Width="525">

<Grid>
    <Label Content="" HorizontalAlignment="Left" Margin="10,64,0,0" VerticalAlignment="Top" Width="497"/>
</Grid>

</Window>

在我的App.xaml.cs中,我有:

namespace UnionPayExhangeRate3
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();  
            //Change the label in MainWindow              
        }
    }
}

我没有看到任何识别标签的方法,所以我是否必须为标签设置ID并以某种方式调用它?

1 个答案:

答案 0 :(得分:1)

Name属性添加到Label

<Label x:Name="myLabel" Content="" HorizontalAlignment="Left" Margin="10,64,0,0" VerticalAlignment="Top" Width="497"/>

所以你可以在这里访问它

 protected override void OnStartup(StartupEventArgs e)
 {
        base.OnStartup(e);
        MainWindow mainWindow = new MainWindow();
        mainWindow.Show();  
        //Change the label in MainWindow              
        mainWindow.myLabel.Content = "Hello World";
 }