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

后端 未结 5 1718
无人及你
无人及你 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:51

    You should implement INotifyPropertyChanged in your "MainWindow" so your "myTextBlock" can automatically pick up changes from your data and update.

    So your "MainWindow" should look like:

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private string _myText;
    
        public string myText { 
          get{return _myText;}
          set{_myText = value;
             if(PropertyChanged!=null) PropertyChanged(this, new PropertyChangedEventArgs("myText")) ;
          }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        etc.....
    }
    

提交回复
热议问题