我没有使用视图模型,有一个类似的帖子使用视图模型,我试图基于此执行此操作,但它不起作用,WPF 将文本框绑定到字典条目
当我单击我的按钮时,我创建了一个列表,然后形成了一个字典,因此在我的修剪示例中使其工作,我在按钮单击时创建了要创建的字典
代码隐藏
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
private Dictionary<string, string> _MyDict;
public Dictionary<string, string> MyDict {
get { return _MyDict; }
set { _MyDict= value; }
}
private void button_Click(object sender, RoutedEventArgs e)
{
MyDict = new Dictionary<string, string>
{
{"First", "Test1"},
{"Second", "Test2"}
};
}
}
XAML
Title="MainWindow" Height="350" Width="674.167">
<Grid>
<Button x:Name="createListfromTextFile" Content="hi" Click="button_Click"/>
<TextBox x:Name="textBox1" Text="{Binding MyDict[First], UpdateSourceTrigger=PropertyChanged}" Height="279" >
</Grid>
更新
根据Chrille的评论,我解决了这个问题,通过实现如下INotifyPropertyChanged
:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public Dictionary<string, string> MyDict
{
get { return _MyDict; }
set {
_MyDict = value;
if (null != PropertyChanged)
{
this.PropertyChanged(this, new PropertyChangedEventArgs ("MyDict"));
}
}
}
}
MainWindow 类 (DataContext) 需要实现 INotifyPropertyChanged。
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public Dictionary<string, string> MyDict
{
get { return _MyDict; }
set
{
_MyDict = value;
PropertyChanged?.(this, new PropertyChangedEventArgs("MyDict"));
}
}
}
或者更好的是,为视图模型创建一个单独的类,并从 ViewModelBase 或类似对象继承:
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged(params string[] propertyName)
{
foreach (var prop in propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(prop));
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, args);
}
}
}
最后在二传手:
public Dictionary<string, string> MyDict
{
get { return _MyDict; }
set
{
_MyDict = value;
NotifyPropertyChanged("MyDict");
}
}
根据Chrille的评论,我通过实现INotifyPropertyChanged解决了这个问题,如下所示:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public Dictionary<string, string> MyDict
{
get { return _MyDict; }
set {
_MyDict = value;
if (null != PropertyChanged)
{
this.PropertyChanged(this, new PropertyChangedEventArgs ("MyDict"));
}
}
}
}
感谢您的帮助!
我在你的代码中发现的主要原因是,你忘记了实现接口,如果你以正确的方式实现它们意味着你的代码将得到工作。请尝试以下操作:
public partial class MainWindow : Window, INotifyPropertyChanged
{
// include your code here
// change the property definition as like this:
public Dictionary<string, string> MyDict
{
get { return _MyDict; }
set
{
_MyDict= value;
NotifyPropertyChanged("MyDict");
}
}
// interface implementation
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}