我无法将数据绑定到WPF / XAML中的局部变量
我想要一个文本框在我点击它时显示变量的值(1到100的迭代),我不知道我在做什么错了:
当我运行项目时,文本框中不显示任何内容。
在文本框中显示变量的最佳方法是什么?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace dataBindingTest { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public string myText { get; set; } public void Button_Click_1(object sender, RoutedEventArgs e) { int i = 0; for (i = 0; i < 100; i++) { myText = i.ToString(); } } } }
XAML:
您的当前myText
属性无法在其值发生更改时通知WPF绑定系统,因此TextBlock
不会更新。
如果将其设为依赖项属性,则会自动实现更改通知,并且对属性的更改将反映在TextBlock
。
所以如果你替换public string myText { get; set; }
public string myText { get; set; }
使用所有这些代码它应该工作:
public string myText { get { return (string)GetValue(myTextProperty); } set { SetValue(myTextProperty, value); } } // Using a DependencyProperty as the backing store for myText. This enables animation, styling, binding, etc... public static readonly DependencyProperty myTextProperty = DependencyProperty.Register("myText", typeof(string), typeof(Window1), new PropertyMetadata(null));
实现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:
并且不要忘记添加窗口的DataContext属性:
试试这个:
public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); this.DataContext = this; } public string myText { get; set; } public void Button_Click_1(object sender, RoutedEventArgs e) { BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += delegate { int i = 0; for (i = 0; i < 100; i++) { System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)(() => { myText = i.ToString(); OnPropertyChanged("myText"); })); Thread.Sleep(100); } }; bw.RunWorkerAsync(); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } }
XAML文件:
您应该在“MainWindow”中实现INotifyPropertyChanged
,这样您的“myTextBlock”就可以自动从您的数据中获取更新并进行更新。
所以你的“MainWindow”应该是这样的:
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..... }
您需要使属性告诉它已更新的绑定。 执行此操作的标准方法是:
- 实现
INotifyPropertyChanged
- 使myText属性成为
DependencyProperty
- 另一种可能不太常用的方法是手动引发事件,如下所示:
public void Button_Click_1(object sender, RoutedEventArgs e) { myText = "Clicked"; BindingOperations.GetBindingExpressionBase(myTextBox, TextBlock.TextProperty).UpdateTarget(); }
请注意,您的TextBlock
具有令人困惑的名称myTextBox