I can't Data Bind to a local variable in WPF/XAML

后端 未结 5 1701
无人及你
无人及你 2021-01-01 01:14

I want a textbox to display the value of a variable when I click it (an iteration of 1 to 100), I do not know what I am doing Wrong:

When I run the project nothing i

5条回答
  •  死守一世寂寞
    2021-01-01 01:56

    implement INotifyPropertyChanged:

    public partial class MainWindow : Window, INotifyPropertyChanged
        {
            public MainWindow()
            {
                this.InitializeComponent();
            }
    
            private string _txt;
            public string txt
            {
                get
                {
                    return _txt;
                }
                set
                {
                    if (_txt != value)
                    {
                        _txt = value;
                        OnPropertyChanged("txt");
                    }
                }
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                txt = "changed text";
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    

    XAML:

    
    
    

    and don't forget about adding the DataContext property of your window:

    
    

提交回复
热议问题