WPF中DataGrid控件的数据绑定与显示数组

1 < DataGrid x:Name ="DataGrid" AutoGenerateColumns ="False" Visibility ="Visible" IsReadOnly ="False" VerticalAlignment ="Top" Height ="307" Canvas.Left ="208" Canvas.Top ="15" Width ="280" >
2 < DataGrid.Columns >
3 < DataGridTextColumn Header ="星期一" Binding =" {Binding 星期一} " Width ="100" />
4 < DataGridTextColumn Header ="星期二" Binding =" {Binding 星期二} " Width ="70" />
5 < DataGridTextColumn Header ="星期三" Binding =" {Binding 星期三} " />
6 < DataGridTextColumn Header ="星期四" Binding =" {Binding 星期四} " />
7 < DataGridTextColumn Header ="星期五" Binding =" {Binding 星期五} " />
8 < DataGridTextColumn Header ="星期六" Binding =" {Binding 星期六} " />
9 < DataGridTextColumn Header ="星期天" Binding =" {Binding 星期天} " />
10 </ DataGrid.Columns >
11 </ DataGrid >

AutoGenerateColumns="False"这个很重要,曾在这个问题上绕很长时间,如果该属性为true既允许插入列标题那结果会是在原来的列后面在加同样的一列。

上面是使用的 <DataGridTextColumn/>给DataGrid添加的固定列标题名,这里注意后面的 Header="星期一" Binding="{Binding 星期一} 其中Binding{}就是关键的数据绑定的那块,这里在{}里的内容就是我们要在C#中定义的结构体或其他的成员名,两者必须一致。下面贴一下 定义的结构体共2个:

1 public struct ProgramStruct
2 {
3 [MarshalAs (UnmanagedType.ByValArray, SizeConst = 50 )] public char [] name; // c char[]
4   public int NumberOf; // c int
5   public uint Fre; // c unsigned long
6   public ushort ser; // c unsigned short
7   public byte sta; // c unsigned char
8   public char nt; // c char
9   public byte dem; // c unsigned char
10   public ushort Servi;
11 [MarshalAs (UnmanagedType.ByValArray, SizeConst = 200 )] public char [] net; // c char[]重点在C#里的指定大小的数组定义
12   public int mf; // c int
13   public int Bandwid; // c int
14   public bool bChe; // c bool
15   }

上面这个是咱们调用dll里的接口需要的参数结构定义。

1 public struct ProgramStruct2
2 {
3 public string   星期一{ get ; set ;}
  4   public string   星期二{ get ; set ;}
5   public ushort  星期三{ get ; set ;}
6   public uint   星期四{ get ; set ;}
7   public byte  星期五{ get ; set ;}
8   public int 星期六{ get ; set ;}
9   public int 星期天{ get ; set ;}
10   }

这里注意那个{get;set;}也是获取值的关键。

下面就是根据咱们使用的数组大小定义上面结构体的数组了以及取值。

1 int count = ImportInterface.GetProgramCount();//调用DLL库里获取文件的个数的接口

5
6 int size = Marshal.SizeOf( typeof (ImportInterface.ProgramStruct)) * count;
7 IntPtr pBuff = Marshal.AllocHGlobal(size);
8 ImportInterface.GetProgramList(pBuff); //调用库获取文件(其中库统一放在ImportInterface.cs里)
9 List < ProgramStruct2 > service = new List < ProgramStruct2 > ();
10 ImportInterface.ProgramStruct[] pClass = new ImportInterface.ProgramStruct[count];
11 ProgramStruct2 ps = new ProgramStruct2();
12
13 for ( int i = 0 ; i < count; i ++ )
14 {
15 IntPtr pPonitor = (IntPtr)(pBuff.ToInt32() + Marshal.SizeOf( typeof (ImportInterface.ProgramStruct)) * i);
16 pClass[i] = (ImportInterface.ProgramStruct)Marshal.PtrToStructure(pPonitor, typeof (ImportInterface.ProgramStruct));
17 ps.星期一 = new string (pClass[i].net);
18 ps.星期二 = new string (pClass[i].name);
19 ps.星期三 = pClass[i].servi;
20 ps.星期四 = pClass[i].Fre;
21 ps.星期五 = pClass[i].sta;
22 ps.星期六 = ( int )pClass[i].nt;
23 ps.星期天 = pClass[i].mf;
24 service.Add(ps);//关键
25
}
39
40 Marshal.FreeHGlobal(pBuff);
41 DataGrid.ItemsSource = service;//关键_这里的DataGrid是DataGrid的ID

列一下ImportInterface.cs的库的引入:

1 [DllImport( " LOVI.dll " )]
2 public static extern int GetProgramCount( );
3     
4     [DllImport("LOVI.dll", CharSet = CharSet.Ansi)]
5      public static extern  void GetProgramList(IntPtr ps);//ProgramStruct *ps

引入库dll要加头文件 using System.Runtime.InteropServices;

 

搞定,其中的有关结构体的成员的名字由于是实际项目所以对其中的命名有所改动!!!

转载于:https://www.cnblogs.com/wainiwann/archive/2011/01/28/1946864.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值