词典 - 文本框绑定不起作用

Dictionary - TextBox binding is not working

我没有使用视图模型,有一个类似的 post 使用视图模型,我尝试基于它来做,但它不起作用, WPF binding textbox to dictionary entry

当我点击我的按钮时,我创建了一个列表,然后形成了一个字典,所以在我精简的例子中为了让它工作,我在点击按钮时创建了字典

代码隐藏

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"));
            }
        }          
     }
  }

我在你的代码中发现的主要原因是,你忘记了实现接口,如果你以正确的方式实现它们意味着你的代码将会工作。请尝试以下操作:

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));
        }
    }
}

根据 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 class (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"));
        }         
    }
}

或者更好的是,为视图模型创建一个单独的 class 并继承自 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);
            }
        }
    }

最后在 setter:

public Dictionary<string, string> MyDict
{
    get { return _MyDict; }
    set 
    {
        _MyDict = value;
        NotifyPropertyChanged("MyDict");
    }         
}