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) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用 ...
随机推荐
- Java反编译器安装及各版本介绍
JAVA语言是1995年5月由SUN公司发布的,由于其安全性高.代码优化.跨平台等特性,迅速取代了很多传统高级语言,占据了企业级网络应用开发等诸多领域的霸主地位. 不过,JAVA最突出 ...
- 大区间素数筛选 POJ2689
题意: 给一个区间[L,U],(1<=L< U<=2,147,483,647),U-L<=1000000,求出[L,U]内距离近期和距离最远的素数对. 因为L,U都小于2^32 ...
- keystone系列二:keystone源码分析
六 keystone架构 6.1 Keystone API Keystone API与Openstack其他服务的API类似,也是基于ReSTFul HTTP实现的. Keystone API划分为A ...
- mvc给html扩展方法:
mvc给html扩展方法: 注意:扩展方法和所在的类都必须是 public static如果在页面直接使用新扩展的方法,需要web.config里把Web.Helper名称命名空间加上,页面才能访问到 ...
- 在 ServiceModel 客户端配置部分中,找不到引用协定“PmWs.PmWebServiceSoap”的默认终结点元素
System.Exception: ConfigManager.LoadConfigurationFromDb ServiceFactory.GetPmWebService 在 ServiceMode ...
- OC加强-day06
#program mark - 08 NSMutableDictionary的使用 [掌握] "/08 NSMutableDictionary的使用/1_练习 "练习 1.小明的身 ...
- 使用AFNetworking进行图片上传
转载自:http://blog.csdn.net/a645258072/article/details/51728806 项目中,我们经常会用到上传图片的功能,而目前的上传图片分为两种(我只知道两种, ...
- C/C++随机数rand()和种子函数srand()
在计算机编程中,常常要产生一个随机数.但是要让计算机产生一个随机数并不那么容易.计算机的执行,是以代码来进行的,所以并不可能像抽牌,扔骰子那样产生一个真正具有随机意义的数.只可能以一定的算法产生一个伪 ...
- 如何在Angular2中使用jquery
首先在index.html中引入jquery文件 <script src="http://cdn.bootcss.com/jquery/2.1.3/jquery.js"> ...
- 使用Eclipse提供的Axis1.x生成WSDL文件以及Server和Client代码
使用Eclipse自带的Axis 1.x来创建一个web service应用的服务端和客户端 Axis 是SOAP WebService协议实现,SOAP实质上是一个基于HTTP POST的请求,以X ...