内容未出现在数据网格中
content does not appear in datagrid
好吧,我有一个数据 class,其中包含一些这样的数据对象:
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");
}
}
并且在 MainWindow 中我添加了一个 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>
对于整个 Window,datakontext 设置为数据 class。在我调用的构造函数中 "DataContext = theData";我在数据 class 的构造函数中添加了一些值,并由 运行 程序证明了此 class 的实例。这些值已正确添加到 ObservableCollection。
但是这些值没有显示在数据网格中。为什么?
尝试设置,
this.DataContext = theData;
您需要为 ItemsSource 设置正确的 属性。
ItemsSource="{Binding User}"
上一行将清除问题。
此外,您应该在 Setter.
中通知 public 属性
Notify("Check");
Notify("User");
DataGrid 的 ItemsSource 属性 应设置或绑定到 IEnumerable<T>
。 DataGrid 中的单个列应该绑定到 T
类型的 属性。您正在尝试将 DataGridTextColumn 绑定到 ObservableCollection<string>
并将 DataGridCheckBoxColumn 绑定到 ObservableCollection<bool>
,这是没有意义的。它们应该分别绑定到 string
和 bool
属性。请参考下面的示例代码。
型号:
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>
好吧,我有一个数据 class,其中包含一些这样的数据对象:
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");
}
}
并且在 MainWindow 中我添加了一个 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>
对于整个 Window,datakontext 设置为数据 class。在我调用的构造函数中 "DataContext = theData";我在数据 class 的构造函数中添加了一些值,并由 运行 程序证明了此 class 的实例。这些值已正确添加到 ObservableCollection。
但是这些值没有显示在数据网格中。为什么?
尝试设置,
this.DataContext = theData;
您需要为 ItemsSource 设置正确的 属性。
ItemsSource="{Binding User}"
上一行将清除问题。 此外,您应该在 Setter.
中通知 public 属性Notify("Check");
Notify("User");
DataGrid 的 ItemsSource 属性 应设置或绑定到 IEnumerable<T>
。 DataGrid 中的单个列应该绑定到 T
类型的 属性。您正在尝试将 DataGridTextColumn 绑定到 ObservableCollection<string>
并将 DataGridCheckBoxColumn 绑定到 ObservableCollection<bool>
,这是没有意义的。它们应该分别绑定到 string
和 bool
属性。请参考下面的示例代码。
型号:
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>