字典 - TextBox绑定不起作用

时间:2017-01-13 09:16:18

标签: c# wpf mvvm inotifypropertychanged

我没有使用视图模型,有一个使用视图模型的类似帖子,我尝试基于它做但它不起作用, 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"));
            }
        }          
     }
  }

3 个答案:

答案 0 :(得分:1)

按照Chrille的评论我通过像这样实现INotifyPropertyChanged来解决问题:

var date = new Date('1970-01-01');  //or your date here
console.log((date.getMonth() + 1) + '/' + date.getDate() + '/' +  date.getFullYear());

感谢您的帮助!

答案 1 :(得分:1)

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

甚至更好,为viewmodel创建一个单独的类,并从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");
    }         
}

答案 2 :(得分:0)

我在您的代码中找到的主要原因是,您忘记实现界面,如果以适当的方式实现它们意味着您的代码将得到实现。请尝试以下方法:

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