简体   繁体   中英

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM