2011-07-03 49 views
0

我正在vs2010中工作。 我已經創建了一個DataGrid,其範圍爲 ObservableCollection List;WPF DataGrid-DataGridCheckBoxColumn vs2010 c#.net

的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"> 

其更新布爾代表之一 -

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

我不明白爲什麼不更新的布爾列.... 任何人都可以請幫助? 謝謝。

回答

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

不幸的是你不能使用自動:

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

非常感謝,我現在就試試。 –

+0

它的作品,非常感謝!!!!!!! –

1

你應該在Class_RetrieveCommand這樣實現INotifyPropertyChanged屬性瞭然後(除了你訴諸代理髮起人)。

+0

非常感謝你! –