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使用的更多相关文章

  1. WPF DataGrid常用属性记录

    WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...

  2. WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL

    In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...

  3. WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

    WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...

  4. xceed wpf datagrid

    <!--*********************************************************************************** Extended ...

  5. 获取wpf datagrid当前被编辑单元格的内容

    原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...

  6. WPF DataGrid绑定一个组合列

    WPF DataGrid绑定一个组合列 前台: <Page.Resources>        <local:InfoConverter x:Key="converter& ...

  7. WPF DataGrid自定义样式

    微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...

  8. WPF DataGrid显格式

    Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL    4.83 (13 votes) ...

  9. WPF DataGrid Custommization using Style and Template

    WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...

  10. 编写 WPF DataGrid 列模板,实现更好的用户体验

    Julie Lerman 下载代码示例 最近我在为一个客户做一些 Windows Presentation Foundation (WPF) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用 ...

随机推荐

  1. 机房收费系统(VB.NET)——存储过程实战

    最初接触存储过程是在耿建玲老师的视频里,当初仅仅是草草过了一遍.仅仅是有了个印象.知道了这个名词:大二时也有SqlServer数据库这门课,只是老师没讲,自己也没看:真正对存储过程的了解来自于自学考试 ...

  2. 安装Rational Rose启动报错:无法启动此程序,由于计算机中丢失 suite objects.dll。

    执行Rational Rose的时候假设出现这种错误,先检查环境变量有没有common的地址,假设没有直接配上就OK:配置例如以下:D:\Program Files\Rational\Common; ...

  3. mvn命令安装jar包--转

    有的jar通过pom配置文件不能下载,比如oracle的jdbc连接jar就是,这个时候需要通过mvn的安装命令进行手动安装jar.命令是: mvn install:install-file -Dfi ...

  4. HTML增加删除邮件(table)

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. Android Activiy的作用

    在Android应用程序中 ,Activity主要的负责创建窗口的,一个Activicy就是代表一个单独的屏幕,并且是用户唯一可以看到的东西 也就是说Activity就是用来实现和用户交互的,就和.n ...

  6. Linux Bash终端快捷键小结

    Ctrl + A  定位至行首 Ctrl + E  定位至行尾 Ctrl + U  向前删除至行首 Ctrl + K  向后删除至行尾 Ctrl + L  清屏

  7. css3绘制中国结

    <!doctype html> <html> <head> <title></title> <meta charset='utf-8' ...

  8. css怎么引用某张图片?链接要怎么写

    总结一下 <a href="D:\xxx"> <img src="./xxx.jpg">

  9. 20151216JqueryUI学习笔记---按钮

    按钮(button) , 可以给生硬的原生按钮或者文本提供更多丰富多彩的外观. 它不单单可以设置按钮或文本,还可以设置单选按钮和多选按钮.一. 使用 button 按钮使用 button 按钮 UI ...

  10. IEnumerable接口的扩展方法

    /// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...