Well, I have a data class with some data objects like this:
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");
}
}
And in the MainWindow I added a DataGrid like this:
<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>
For the whole Window the datakontext ist set to the data class. In the constructor I called "DataContext = theData"; I added some values in the constructor of the data class and proofed by running the program the instance of this class. The values are correct added to the ObservableCollection.
But the values are not shown in the datagrid. Why?
The ItemsSource property of the DataGrid should be set or bound to an IEnumerable<T>
. And a single column in the DataGrid should be bound to a property of the type T
. You are trying to bind a DataGridTextColumn to an ObservableCollection<string>
and a DataGridCheckBoxColumn to an ObservableCollection<bool>
and this makes no sense. They should be bound to a string
and bool
property respectively. Please refer to the following sample code.
Model:
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));
}
}
View Model:
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; }
}
View:
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>