Question

In my MainWindow.xaml, I have:

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

In my App.xaml.cs, I have:

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              
        }
    }
}

I don't see any way to identify the label so do I have to set an id for the label and call it somehow?

Was it helpful?

Solution

Add Name property to your Label

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

So you can access it here

 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";
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top