【问题标题】:WPF DataGrid-DataGridCheckBoxColumn vs2010 c# .netWPF DataGrid-DataGridCheckBoxColumn vs2010 c#.net
【发布时间】:2011-09-27 13:20:49
【问题描述】:

我在 vs2010 工作。 我创建了一个绑定到的 DataGrid ObservableCollection 列表;

Class_CMD 看起来像这样:

 public class Class_RetrieveCommand
{
    public string CMD { get; set; }
    public bool C_R_CMD { get; set; }
    public bool S_CMD { get; set; }
    public bool C_S_CMD { get; set; }
}

我有 4 个委托传递给另一个窗口,这个窗口需要在运行时更新列表。在运行时,我可以看到网格的字符串列一直在更新,但 DataGridCheckBoxColumns 永远不会更新。

数据网格 -

<DataGrid Background="Transparent" x:Name="DataGrid_CMD" Width="450" MaxHeight="450" Height="Auto" ItemsSource="{Binding}" AutoGenerateColumns="True">

更新布尔值的代表之一是 -

 public void UpdateC_S_CMD(string Msg)
    {
        foreach (Class_CMD c in List.ToArray())
        {
            if (c.CMD.Equals(Msg))
                c.C_S_CMD = true;
        }
    }

我不明白为什么 bool 列没有更新.... 有人可以帮忙吗? 谢谢。

【问题讨论】:

    标签: c# .net wpf visual-studio-2010 datagrid


    【解决方案1】:

    您的 Class_RetrieveCommand 类需要实现 INotifyPropertyChanged 接口。否则,数据绑定到类实例的各个行不知道基础属性已更改。如果你把它改成这样,你应该会在你的网格中看到变化:

    public class Class_RetrieveCommand : INotifyPropertyChanged
    {
        private bool _cRCmd;
        private bool _cSCmd;
        private string _cmd;
        private bool _sCmd;
    
        public string CMD
        {
            get { return _cmd; }
            set
            {
                _cmd = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("CMD"));
            }
        }
    
        public bool C_R_CMD
        {
            get { return _cRCmd; }
            set
            {
                _cRCmd = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("C_R_CMD"));
            }
        }
    
        public bool S_CMD
        {
            get { return _sCmd; }
            set
            {
                _sCmd = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("S_CMD"));
            }
        }
    
        public bool C_S_CMD
        {
            get { return _cSCmd; }
            set
            {
                _cSCmd = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("C_S_CMD"));
            }
        }
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    
        public void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您应该像这样在Class_RetrieveCommand 中实现INotifyPropertyChanged

      public class Class_RetrieveCommand : INotifyPropertyChanged
      {
          private string _CMD;
          public string CMD 
          {
              get { return _CMD; } 
              set { _CMD = value; OnPropertyChanged("CMD"); }
          }
      
          ... similar for the other properties
      
          public event PropertyChangedEventHandler PropertyChanged;
          private void OnPropertyChanged(string propertyName)
          {
              var handler = PropertyChanged;
              if (handler != null)
              {
                  handler(this, new PropertyChangedEventArgs(propertyName));
              }
          }
      }
      

      不幸的是,你不能再使用自动属性(除非你求助于代理生成器)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-22
        • 2018-06-20
        • 2011-05-31
        • 2017-11-17
        • 2012-07-07
        相关资源
        最近更新 更多