平台 :Vs 2010,Blend 4
OK,代码下载:https://files.cnblogs.com/Royal_WH/BlogWebTest.rar
技术性不是很强,但是很实用,题目想好了,明天来写,以后每天补充一点吧!
好了,先来介绍DataGrid吧,毕竟大家都要用么!
先看下他的属性吧(对于画笔、外观、布局方面的属性我就不介绍了,毕竟是一些公共属性想必大家已经比较了解了):
重要属性:
1.ItemSource 设置数据源。
用法: dataGrid.ItemSource = 数据源集合;
2.AutoGenerateColumns 是否在绑定数据源后自动生成列。
用法:直接在Xmal中附加上这一属性 true or false。
3.Columns 不用多说了,就是列集合。
用法:在Xmal中写入以下内容
<sdk:DataGridTextColumn/>
</sdk:DataGrid.Columns>
当然这里的sdk:DataGridTextColumn也可以是模板列和CheckBoxColumn,关于模板列我们后面再讲解。
4.HeadersVisibility 标题是否可见。
用法:xmal中写,All是行列都可见,None是都不可见,Column和Row分别是列和行可见。
5.GridLinesVisibility 是否显示网格线
用法:Xmal中写,All是行列都可见,None是都不可见,Horizontal是行可见,Vertical是列可见
6.SelectedIndex 当前选中的索引编号
用法:C# 中 dataGrid.SelectedIndex 获取或设置
7.SelectionMode 当前选择模式单选(single)或多选(Extended)
用法:Xmal中写标记
8.CanUserReorderColumns 是否可拖动列和改变列顺序
用法:Xmal中写标记
9.CanUserResizeColumns 是否可以改变列的大小
用法:Xmal中写标记
好了还有一些什么 IsReadyOnly,IsEnabled,IsHitTestVisible,CanUserSortColumns大家一看就明白的我就不过多讲了!
下面写个绑定数据的小例子,由于是测试我就不用WCF或WebService来传数据了。先上图:
Xmal 代码:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="419" d:DesignWidth="642" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White" Height="357" Width="594">
<sdk:DataGrid x:Name="dataGrid" Height="169" HorizontalAlignment="Left" Margin="88,48,0,0" VerticalAlignment="Top" Width="400" AutoGenerateColumns="True"/>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="88,272,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
</Grid>
</UserControl>
C# 代码:
{
List<ModelTest> temp =new List<ModelTest>();
public MainPage()
{
InitializeComponent();
}
void LoadData()
{
ModelTest newModel =new ModelTest();
newModel.ID =1;
newModel.Name ="张三";
newModel.RegData = DateTime.Now;
newModel.Sex =0;
temp.Add(newModel);
newModel =new ModelTest();
newModel.ID =2;
newModel.Name ="李四";
newModel.RegData = DateTime.Now;
newModel.Sex =0;
temp.Add(newModel);
newModel =new ModelTest();
newModel.ID =3;
newModel.Name ="小王";
newModel.RegData = DateTime.Now;
newModel.Sex =1;
temp.Add(newModel);
}
privatevoid button1_Click(object sender, RoutedEventArgs e)
{
LoadData();
dataGrid.ItemsSource = temp;
}
}
{
publicint ID
{ set; get; }
publicstring Name
{ set; get; }
public DateTime RegData
{ set; get; }
publicbyte Sex
{ set; get; }
publicstring Image
{ set; get; }
}
一.获取内容
OK,我们看下如何获取选中框的内容:
C# 代码:
{
if (dataGrid.SelectedItem !=null)
{
ModelTest model = dataGrid.SelectedItem as ModelTest;
MessageBox.Show(model.Name);
}
}