WPF DataGrid-DataGridCheckBoxColumn vs2010 c#.net

时间:2011-07-03 08:11:17

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

我在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 -

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

更新bool的代表之一是 -

 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列没有更新.... 有人可以帮忙吗? 感谢。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

您应该在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));
        }
    }
}

很遗憾,您不能再使用自动属性了(除了您使用代理生成器)。