2017-01-09 61 views
0

嗯,我有一些像這樣的數據對象的數據類:內容不會出現在數據網格

private ObservableCollection<bool> check = new ObservableCollection<bool>(); 
    public ObservableCollection<bool> Check 
    { 
     get { return check; } 
     set 
     { 
      check = value; 
      Notify("check"); 
     } 
    } 

    private ObservableCollection<string> user = new ObservableCollection<string>(); 
    public ObservableCollection<string> User 
    { 
     get { return user; } 
     set 
     { 
      user = value; 
      Notify("user"); 
     } 
    } 

而在主窗口我加了這樣一個DataGrid:

<DataGrid AutoGenerateColumns="False" 
     Name="dataGrid1" 
     CanUserAddRows="False" CanUserSortColumns="False" CanUserResizeColumns="True" CanUserReorderColumns="False" 
     ItemsSource="{Binding}"> 
     <DataGrid.Columns > 
      <DataGridCheckBoxColumn Header = "" Binding="{Binding Check, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" /> 
      <DataGridTextColumn Header = "User" Binding="{Binding User, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" /> 
     </DataGrid.Columns> 
    </DataGrid> 

對於整個窗口datakontext ist設置爲數據類。在構造函數中,我調用了「DataContext = theData」;我在數據類的構造函數中添加了一些值,並通過運行程序來驗證此類的實例。值正確添加到ObservableCollection。

但是這些值沒有顯示在數據網格中。爲什麼?

回答

1

DataGrid的ItemsSource屬性應設置或綁定到IEnumerable<T>。 DataGrid中的單個列應綁定到類型爲T的屬性。您試圖將DataGridTextColumn綁定到ObservableCollection<string>,將DataGridCheckBoxColumn綁定到ObservableCollection<bool>,這沒有任何意義。他們應分別綁定到stringbool屬性。請參考下面的示例代碼。

型號:

public class YourDataObject : INotifyPropertyChanged 
{ 
    private bool _check; 
    public bool Check 
    { 
     get { return _check; } 
     set { _check = value; NotifyPropertyChanged(); } 
    } 

    private string _user; 
    public string User 
    { 
     get { return _user; } 
     set { _user = value; NotifyPropertyChanged(); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

視圖模型:

public class ViewModel 
{ 
    public ViewModel() 
    { 
     TheDataObjects = new ObservableCollection<YourDataObject>(); 
     TheDataObjects.Add(new YourDataObject()); 
     TheDataObjects.Add(new YourDataObject()); 
     TheDataObjects.Add(new YourDataObject()); 
    } 
    public ObservableCollection<YourDataObject> TheDataObjects { get; private set; } 
} 

查看:

public MainWindow() 
{ 
    InitializeComponent(); 
    DataContext = new ViewModel(); 
} 

<DataGrid AutoGenerateColumns="False" 
    Name="dataGrid1" 
    CanUserAddRows="False" CanUserSortColumns="False" CanUserResizeColumns="True" CanUserReorderColumns="False" 
    ItemsSource="{Binding TheDataObjects}"> 
    <DataGrid.Columns > 
     <DataGridCheckBoxColumn Header = "" Binding="{Binding Check, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" /> 
     <DataGridTextColumn Header = "User" Binding="{Binding User, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" /> 
    </DataGrid.Columns> 
</DataGrid> 
+0

好吧,我寫了一個新的YourDataObject calss實現了我的現有數據類旁邊的一些其他數據的Veiw模型。這有點不同,但我更多地瞭解它如何工作,我很高興! – tux007

0

嘗試設置,

this.DataContext = theData; 
+0

謝謝,但它也不起作用。 – tux007

0

您需要正確的屬性設置爲的ItemsSource。

ItemsSource="{Binding User}" 

上方行將清除問題。 另外,您應該通知Setter中的公共屬性。

Notify("Check"); 
Notify("User"); 
+0

好吧,我想我必須通知setter中的私有屬性。我的例子工作正常的文本框,現在我需要第二個維度。 – tux007