I have an array:
我有一个数组:
string Companies[,] = new string[100,7];
How can I put it to a dataGrid? I can't find any answear which works and I don't know where to start. I'm new to WPF, so can someone explain it to me, please?
我怎样才能把它放到dataGrid中?我找不到任何有效的衣服,我不知道从哪里开始。我是WPF的新手,有人可以向我解释一下吗?
2 个解决方案
#1
2
Please use ItemsSource to assign data collections. I suggest you to read about MVVM implementation for WPF. But to start...
请使用ItemsSource分配数据集合。我建议你阅读WPF的MVVM实现。但要开始......
Create a class that implements INotifyPropertyChanged interface
创建一个实现INotifyPropertyChanged接口的类
public class Employer : INotifyPropertyChanged
{
private string nameField;
public string Name {
get { return nameField; }
set {
nameField= value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
private int idField;
public int Id {
get { return idField; }
set {
idField= value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Id"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
Create a property
创建一个属性
private ObservableCollection<Employer> employersField;
public ObservableCollection<Employer> Employers
{
get { return employersField; }
set {
employersField= value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Employers"));
}
}
}
Now let's say in a constructor you do
现在让我们说你做的构造函数
Employers = new ObservableCollection<Employer> {
new Employer {
Id = 0,
Name = "Mike"
},
new Employer {
Id = 1,
Name = "Dave"
}
}
Let's assume that you don't have view class so all your properties in a xaml related cs file. So you need to bind the DataContext
property of your dataGrid to your class and after assign ItemsSource to your property
假设您没有视图类,因此xaml相关的cs文件中的所有属性都是如此。因此,在将ItemsSource分配给属性之后,需要将dataGrid的DataContext属性绑定到类
<DataGrid DataContext = {Binding ElementName=YourControlName} ItemsSource="{Binding Employers}">
your content
</DataGrid >
YourControlName is a userControl name in xaml!
YourControlName是xaml中的userControl名称!
<UserControl x:Name="YourControlName" >
all stuff
</UserControl>
Look, this is a vary short overview, because I didn't show you how to bind your class properties to dataGrid columns and also how to bind selectedItem property to your property. But you can find many examples at * as well as on the Internet. I just showed how to start and how things work in WPF
看,这是一个不同的简短概述,因为我没有向您展示如何将类属性绑定到dataGrid列,以及如何将selectedItem属性绑定到您的属性。但是你可以在*和Internet上找到很多例子。我刚刚展示了如何在WPF中启动以及如何工作
#2
0
Constructor List of objects as below use it
构造函数下面的对象列表使用它
class ViewModel
{
public string[,] Companies
{
get;
set;
}
public List<Example> Values
{
get;
set;
}
public ViewModel()
{
Companies = new string[2, 2] { { "sjhbfsjh", "jshbvjs" }, {"vsmvs", "nm vmdz" } };
Values = new List<Example>();
for (int i = 0; i < 2; i++)
{
Example ee = new Example();
ee.A = Companies[i, 0];
ee.B = Companies[i, 1];
Values.Add(ee);
}
}
}
public class Example
{
public string A
{
get;
set;
}
public string B
{
get;
set;
}
}
Then in your Xmal you can do as below
然后在您的Xmal中,您可以执行以下操作
<DataGrid ItemsSource="{Binding Path=Values}"></DataGrid>
Set the data context in Xmal.cs
在Xmal.cs中设置数据上下文
DataContext = new ViewModel();
#1
2
Please use ItemsSource to assign data collections. I suggest you to read about MVVM implementation for WPF. But to start...
请使用ItemsSource分配数据集合。我建议你阅读WPF的MVVM实现。但要开始......
Create a class that implements INotifyPropertyChanged interface
创建一个实现INotifyPropertyChanged接口的类
public class Employer : INotifyPropertyChanged
{
private string nameField;
public string Name {
get { return nameField; }
set {
nameField= value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
private int idField;
public int Id {
get { return idField; }
set {
idField= value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Id"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
Create a property
创建一个属性
private ObservableCollection<Employer> employersField;
public ObservableCollection<Employer> Employers
{
get { return employersField; }
set {
employersField= value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Employers"));
}
}
}
Now let's say in a constructor you do
现在让我们说你做的构造函数
Employers = new ObservableCollection<Employer> {
new Employer {
Id = 0,
Name = "Mike"
},
new Employer {
Id = 1,
Name = "Dave"
}
}
Let's assume that you don't have view class so all your properties in a xaml related cs file. So you need to bind the DataContext
property of your dataGrid to your class and after assign ItemsSource to your property
假设您没有视图类,因此xaml相关的cs文件中的所有属性都是如此。因此,在将ItemsSource分配给属性之后,需要将dataGrid的DataContext属性绑定到类
<DataGrid DataContext = {Binding ElementName=YourControlName} ItemsSource="{Binding Employers}">
your content
</DataGrid >
YourControlName is a userControl name in xaml!
YourControlName是xaml中的userControl名称!
<UserControl x:Name="YourControlName" >
all stuff
</UserControl>
Look, this is a vary short overview, because I didn't show you how to bind your class properties to dataGrid columns and also how to bind selectedItem property to your property. But you can find many examples at * as well as on the Internet. I just showed how to start and how things work in WPF
看,这是一个不同的简短概述,因为我没有向您展示如何将类属性绑定到dataGrid列,以及如何将selectedItem属性绑定到您的属性。但是你可以在*和Internet上找到很多例子。我刚刚展示了如何在WPF中启动以及如何工作
#2
0
Constructor List of objects as below use it
构造函数下面的对象列表使用它
class ViewModel
{
public string[,] Companies
{
get;
set;
}
public List<Example> Values
{
get;
set;
}
public ViewModel()
{
Companies = new string[2, 2] { { "sjhbfsjh", "jshbvjs" }, {"vsmvs", "nm vmdz" } };
Values = new List<Example>();
for (int i = 0; i < 2; i++)
{
Example ee = new Example();
ee.A = Companies[i, 0];
ee.B = Companies[i, 1];
Values.Add(ee);
}
}
}
public class Example
{
public string A
{
get;
set;
}
public string B
{
get;
set;
}
}
Then in your Xmal you can do as below
然后在您的Xmal中,您可以执行以下操作
<DataGrid ItemsSource="{Binding Path=Values}"></DataGrid>
Set the data context in Xmal.cs
在Xmal.cs中设置数据上下文
DataContext = new ViewModel();