wpf:DataGrid使用
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:WpfToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
1. WpfToolkit:DataGrid在TableView.xaml中定义如下
- <WpfToolkit:DataGrid x:Name="PlotViewDataGrid"
- ItemsSource="{Binding DataGridSource, Mode=TwoWay}" IsReadOnly="True" SelectedItem="{Binding DataGridSelected}"
- AutoGenerateColumns="True" AutoGeneratedColumns="PlotViewDataGrid_AutoGeneratedColumns"
- Width="685" RowHeight="25" Margin="0" CanUserResizeRows="True" SelectionMode="Single"
- SelectedIndex="{Binding DataGridSelectedIndex, Mode=TwoWay}" Grid.Row="0">
- <i:Interaction.Triggers>
- <i:EventTrigger EventName="SelectionChanged">
- <i:InvokeCommandAction Command="{Binding GetSelectCommand}"
- CommandParameter="{Binding SelectedItem,ElementName=PlotViewDataGrid}" />
- </i:EventTrigger>
- <i:EventTrigger EventName="MouseDoubleClick">
- <i:InvokeCommandAction Command="{Binding DataGridDoubleClickCommand}"
- CommandParameter="{Binding SelectedItem,ElementName=PlotViewDataGrid}" />
- </i:EventTrigger>
- </i:Interaction.Triggers>
- <WpfToolkit:DataGrid.RowStyle>
- <Style TargetType="{x:Type WpfToolkit:DataGridRow}">
- <Setter Property="Background" Value="Red" />
- </Style>
- </WpfToolkit:DataGrid.RowStyle>
- </WpfToolkit:DataGrid>
1.在TableView.xmal.cs文件中PlotViewDataGrid_AutoGeneratedColumns函数可以改变列宽
private void PlotViewDataGrid_AutoGeneratedColumns(object sender, EventArgs e) {
double dgwidth = this.PlotViewDataGrid.Width;
int columnWidth = this.PlotViewDataGrid.Columns.Count;
this.PlotViewDataGrid.HorizontalContentAlignment = (HorizontalAlignment)1;
for (int i = 0; i < this.PlotViewDataGrid.Columns.Count; i++)
{
this.PlotViewDataGrid.Columns[i].Width = new Microsoft.Windows.Controls.DataGridLength(dgwidth / columnWidth);
}
}
2.在TableViewModel.cs文件中
- #region DataGridDoubleClickCommand
- RelayCommand<object> dataGridDoubleClickCommand = null;
- public ICommand DataGridDoubleClickCommand
- {
- get
- {
- if (dataGridDoubleClickCommand == null)
- {
- dataGridDoubleClickCommand = new RelayCommand<object>((p) => OnDataGridDoubleClickCommand(p), (p) => CanDataGridDoubleClickCommand(p));
- }
- return dataGridDoubleClickCommand;
- }
- }
- private bool CanDataGridDoubleClickCommand(object p)
- {
- return true;
- }
- private void OnDataGridDoubleClickCommand(object p)
- {
- DataGridDoubleClickHandle(p);
- }
- #endregion
3.DataGrid风格自定义
<WpfToolkit:DataGrid.RowStyle>
<Style TargetType="{x:Type WpfToolkit:DataGridRow}">
<Setter Property="Background" Value="Red" />
</Style>
</WpfToolkit:DataGrid.RowStyle>
4.CanUserResizeRows="True"允许用户调整行高
其他参考内容如下:
(1)自动生成列
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><DataGrid AutoGenerateColumns="True" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick"/>
2)取消自动生成列,手动绑定到相应字段
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><DataGrid AutoGenerateColumns="False" Name="datagrid" CanUserAddRows="False" MouseDoubleClick="datagrid_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Header="编号" Binding="{Binding ID}"></DataGridTextColumn>
<DataGridTextColumn Header="公司" Binding="{Binding CompanyName}"></DataGridTextColumn>
<DataGridTextColumn Header="固定资产" Binding="{Binding FixedAssets}" Width ="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
最后一列设置Width ="*"是为了取消空白列。
(3)后台代码
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->privatevoid Window_Loaded(object sender, RoutedEventArgs e)
{
datagrid.ItemsSource = AccessDAL.OleDbHelper.ExecuteDataTable("SELECT * from Customers").DefaultView;
}
//双击DataGrid,显示相应信息
privatevoid datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataRowView row = datagrid.SelectedItem as DataRowView;
MessageBox.Show(row["id"].ToString());
}
//如果绑定到对象集合,如ObservableCollection<Employee>,代码如下:
ObservableCollection<Employee> col;
public EmployeeManage()
{
InitializeComponent();
col =new ObservableCollection<Employee>();
col.Add(new Employee() { Id =, Name ="Jim", Salary =2500.50f });
col.Add(new Employee() { Id =, Name ="John", Salary =2600.50f });
datagrid.ItemsSource = col;
}
privatevoid datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Employee emp=datagrid.SelectedItem as Employee;
MessageBox.Show(emp.Id.ToString());
}
(4)删除选中的多行数据
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->privatevoid Delete_Click(object sender, RoutedEventArgs e)
{
for (int i = datagrid.SelectedItems.Count -; i >=; i--)
{
Good good = datagrid.SelectedItems[i] as Good;
goods.Remove(good);
}
}
wpf:DataGrid使用的更多相关文章
- WPF DataGrid常用属性记录
WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...
- WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL
In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...
- xceed wpf datagrid
<!--*********************************************************************************** Extended ...
- 获取wpf datagrid当前被编辑单元格的内容
原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- WPF DataGrid自定义样式
微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...
- WPF DataGrid显格式
Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL 4.83 (13 votes) ...
- WPF DataGrid Custommization using Style and Template
WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...
- 编写 WPF DataGrid 列模板,实现更好的用户体验
Julie Lerman 下载代码示例 最近我在为一个客户做一些 Windows Presentation Foundation (WPF) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用 ...
随机推荐
- Android View.onMeasure方法的理解(转载)
一下内容转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure(计算)和layout(布局).1 ...
- mysql_config_editor
加入账号: [root@server-mysql bin]# ./mysql_config_editor set --login-path=client --user=root --password ...
- Tsar 服务器系统和应用信息的采集报告工具
Tsar介绍 Tsar是淘宝的一个用来收集服务器系统和应用信息的采集报告工具,如收集服务器的系统信息(cpu,mem等),以及应用数据(nginx.swift等),收集到的数据存储在服务器磁盘上,可以 ...
- iOS开发中常见的语句@synthesize obj=obj的意义详解
我们在进行iOS开发时,经常会在类的声明部分看见类似于@synthesize window=_window; 的语句,那么,这个window是什么,_ window又是什么,两个东西分别怎么用,这是一 ...
- java注解研究
注解作用 常见的作用有以下几种: 生成文档.这是最常见的,也是java 最早提供的注解.常用的有@see @param @return @author等. 跟踪代码依赖性,实现替代配置文件功能.比较常 ...
- php笔记05:http协议中防盗链技术
倘若我们自己在电脑上写了一个网站文件(可以是html,php文件等等),但是只希望本机可以访问这个文件,不希望别的电脑访问就需要这里的防盗链技术 1.我们在本地写了一个import.php文件: 而且 ...
- Java代码安全测试解决方案
Java代码安全测试解决方案: http://gdtesting.com/product.php?id=106
- redhat 6.4 双网卡绑定
linux系统配置 1.redhat 6.4 双网卡绑定 1)#ethtool eth* //在服务器网口接网线至笔记本,确定各网口的配置文件: 2)切换目录 #cd /etc/sysconfig/n ...
- C#相关时间DateTime格式化
C#代码中时间转换为2016-01-24 12:12:12需要如下操作: DateTime.Parse(sj).ToString("yyyy-MM-dd HH:m:ss") 但是O ...
- A Swift Tour(3) - Functions and Closures
Functions and Closures 使用func来声明函数,通过括号参数列表的方式来调用函数,用 --> 来分割函数的返回类型,参数名和类型,例如: func greet(name: ...