Joan Venge
Joan Venge

Reputation: 331460

How to bind a label to a field found in the same window class in WPF?

I have a standard WPF app created using Visual Studio template. My MainWindow code looks like this:

public partial class MainWindow : Window
{
    public MainWindow ( )
    {
        this.InitializeComponent ( );
    }

    string MyValue = "";
}

How can I bind my Label to MyValue using XAML?

Upvotes: 0

Views: 365

Answers (1)

Alfred Manoj
Alfred Manoj

Reputation: 106

This is my .cs file :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
    }

    public string MyValue { get; set; } = "Hello";
}

and my view file :

 <Grid>
    <Label Content="{Binding MyValue}"/>
</Grid>

Upvotes: 1

Related Questions