将数据从2d数组传递到dataGrid wpf C#
我有一个数组:将数据从2d数组传递到dataGrid wpf C#,c#,wpf,arrays,binding,datagrid,C#,Wpf,Arrays,Binding,Datagrid,我有一个数组: string Companies[,] = new string[100,7]; 如何将其放入数据网格?我找不到合适的女装,也不知道从哪里开始。我是WPF的新手,有人能给我解释一下吗 请使用ItemsSource分配数据收集。我建议您阅读关于WPF的MVVM实现。但首先 创建一个实现INotifyPropertyChanged接口的类 public class Employer : INotifyPropertyChanged { private string
string Companies[,] = new string[100,7];
如何将其放入数据网格?我找不到合适的女装,也不知道从哪里开始。我是WPF的新手,有人能给我解释一下吗 请使用ItemsSource分配数据收集。我建议您阅读关于WPF的MVVM实现。但首先 创建一个实现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;
}
创建属性
private ObservableCollection<Employer> employersField;
public ObservableCollection<Employer> Employers
{
get { return employersField; }
set {
employersField= value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Employers"));
}
}
}
YourControlName是xaml中的userControl名称
<UserControl x:Name="YourControlName" >
all stuff
</UserControl>
所有的东西
看,这是一个简短的概述,因为我没有向您展示如何将类属性绑定到dataGrid列,以及如何将selectedItem属性绑定到属性。但是你可以在stackoverflow和互联网上找到很多例子。我刚刚演示了如何在WPF中启动以及如何工作,如下所示的构造函数对象列表使用它
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;
}
}
<UserControl x:Name="YourControlName" >
all stuff
</UserControl>
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;
}
}
<DataGrid ItemsSource="{Binding Path=Values}"></DataGrid>
DataContext = new ViewModel();