前台xaml:
<sdk:DataGrid Margin="0,8,0,138" x:Name="dataGrid" AutoGenerateColumns="False" IsReadOnly="True"后台CS:
AlternatingRowBackground="BlanchedAlmond">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="机构" Binding="{Binding JG}"/>
<sdk:DataGridTextColumn Header="办理柜员" Binding="{Binding BLGY}"/>
<sdk:DataGridTextColumn Header="业务" Binding="{Binding YW}"/>
<sdk:DataGridTextColumn Header="票号" Binding="{Binding PH}"/>
<sdk:DataGridTextColumn Header="到达时间" Binding="{Binding DDSJ}"/>
<sdk:DataGridTextColumn Header="办理时间" Binding="{Binding BLSJ}"/>
<sdk:DataGridTextColumn Header="结束时间" Binding="{Binding JSSJ}"/>
<sdk:DataGridTextColumn Header="等待时长" Binding="{Binding DDSC}"/>
<sdk:DataGridTextColumn Header="服务时长" Binding="{Binding FWSC}"/>
<sdk:DataGridTextColumn Header="驻留时长" Binding="{Binding ZLSC}"/>
<sdk:DataGridTextColumn Header="业务状态" Binding="{Binding YWZT}"/>
<sdk:DataGridTextColumn Header="评价" Binding="{Binding PJ}"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
public MainPage()
{
InitializeComponent();
dataGrid.ItemsSource = new dataSourceList()
}
dataSource类:
public class dataSource
{
public string JG { get; set; }
public string BLGY { get; set; }
public string YW { get; set; }
public string PH { get; set; }
public string DDSJ { get; set; }
public string BLSJ { get; set; }
public string JSSJ { get; set; }
public string DDSC { get; set; }
public string FWSC { get; set; }
public string ZLSC { get; set; }
public string YWZT { get; set; }
public string PJ { get; set; }
}
dataSourceList类:
public class dataSourceList : List<dataSource>
{
private List<string> JSList = new List<string>()
{
"团结湖北区储蓄所",
"三里屯储蓄所",
"团结湖南区储蓄所",
"幸福村储蓄所",
"幸福三村储蓄所",
"雅宝路储蓄所",
"朝阳支行营业部专柜",
"十里堡网点支行",
"甘露园储蓄所",
"八里庄北里储蓄所",
"慈云寺储蓄所",
"八里庄专柜",
"管庄储蓄所",
"定福庄储蓄所",
"双桥新村储蓄所",
"管庄专柜",
"左家庄储蓄所",
"新源里储蓄所",
"顺源街储蓄所",
"三元桥南储蓄所"
};
private List<string> YWList = new List<string>()
{
"开户",
"个人贷款",
"储蓄业务",
"汇款业务",
"跨行转账",
"电子银行服务",
"账户信息查询",
"电子工资单查询",
"信用卡还款",
"投资理财"
};
private List<string> PJList = new List<string>()
{
"非常满意",
"满意",
"基本满意",
"不满意",
"非常不满意"
};
public dataSourceList()
{
Random random = new Random();
int count = JSList.Count;
for (int order = 0; order < count; order++)
{
dataSource data = new dataSource();
data.JG = JSList[order];
if (order < 10)
data.BLGY = "100" + order.ToString();
else
data.BLGY = "10" + order.ToString();
data.YW = YWList[random.Next(10)];
if (order < 10)
data.PH = "A00" + order.ToString();
else
data.PH = "A0" + order.ToString();
DateTime DDSJTime = DateTime.Parse("10:00:00").AddMinutes(order * 2);
data.DDSJ = DDSJTime.ToLongTimeString();
DateTime BLSJTime = DDSJTime.AddSeconds(random.Next(3600));
data.BLSJ = BLSJTime.ToLongTimeString();
DateTime JSSJTime = BLSJTime.AddSeconds(random.Next(3600));
data.JSSJ = JSSJTime.ToLongTimeString();
data.DDSC = (BLSJTime - DDSJTime).ToString();
data.FWSC = (JSSJTime - BLSJTime).ToString();
data.ZLSC = (JSSJTime - DDSJTime).ToString();
data.YWZT = "完成";
data.PJ = PJList[random.Next(5)];
this.Add(data);
}
}
}
笔记【更新中】:
1、DataGrid的ItemsSource是List。
2、像这样,先定义好一个表的列再添加数据源的,需要把每一列的绑定值都写全(Binding="{Binding YW}"),我之前就是只写了3个但是不出结果,写完就出来了。
3、需要设置AutoGenerateColumns="False",否则控件会重复生成数据。
4、需要设置IsReadOnly="True",使之不能做修改但是仍然可以点击。
5、AlternatingRowBackground="BlanchedAlmond"用来设置表格的交替颜色(奇数行和偶数行颜色不同)。
6、如果想设置DataGrid的样式,想在前台就显示数据做参考时,把ItemSource的绑定写在前台就可以了。
7、对于动态生成的DataGrid数据,设置他的列宽。
//LayoutUpdated事件里
private void dataGrid_LayoutUpdated(object sender, EventArgs e){
dataGrid.Columns[1].Width = new DataGridLength(100);
}
8、想要隐藏标题的话,设置 HeadersVisibility="None"。
9、DataGrid默认的分割线是按列来显示的,设置GridLinesVisibility="Horizontal"可以把分割线按行显示。
如果想以虚线显示时,修改DataGrid的RowStyle中的BottomGridLine(是一个Rectangle),设置他的 StrokeDashArray="5,3",Fill="Transparent" ,Stroke="Black" 。