WPF中使用DataGrid时操作列按钮问题
在使用DataGrid的过程中,我们有时候需要对选取的某一行数据进行多个操作,这个时候操作列只有一个按钮显然无法满足我们的要求,我们需要多个按钮才能达到我们的目的。
UI页面代码:
<Grid>
<DataGrid x:Name="datagrid" AutoGenerateColumns="False" ScrollViewer.HorizontalScrollBarVisibility="Hidden" SizeChanged="datagrid_SizeChanged" RowHeaderWidth="0" IsReadOnly="True" BorderBrush="Transparent" BorderThickness="1">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
<Setter Property="FontSize" Value="20"></Setter>
<Setter Property="FontWeight" Value="ExtraBold"></Setter>
<Setter Property="Height" Value="50"></Setter>
</Style>
</DataGrid.ColumnHeaderStyle> <DataGrid.Columns>
<DataGridTextColumn x:Name="UserName" Binding="{Binding Name}" Header="姓名" FontSize="20"/>
<DataGridTextColumn x:Name="UserSex" Binding="{Binding Sex}" Header="性别" FontSize="20"/>
<DataGridTextColumn x:Name="UserAge" Binding="{Binding Age}" Header="是否完成" FontSize="20"/>
<DataGridTextColumn x:Name="UserPhone" Binding="{Binding Phone}" Header="下发时间" FontSize="20"/>
<DataGridTemplateColumn x:Name="UserAction" Header="操作" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button x:Name="BtnAction" Content="{Binding BtnActionStr}" Height="34"
Width="80" Click="BtnAction_Click" IsEnabled="{Binding Enabled}"
FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
</Button>
<Button x:Name="BtnAction1" Content="{Binding BtnActionStr1}" Height="34"
Width="80" Click="BtnAction1_Click" IsEnabled="{Binding Enabled1}"
FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
后台代码:
List<User> users = new List<User>();
//向DataGrid中添加数据
private void GetDataGrid()
{
for (int i = 0; i < 10; i++)
{
User user = new User();
user.Name = "Tom"; user.Sex = "男"; user.Age = "18"; user.Phone = "000000";
user.BtnActionStr = "按钮" + i;
user.BtnActionStr1 = "按钮" + (i + 1);
if (i % 2 == 0)
{
user.Enabled = true;
user.Enabled1 = false;
}
else
{
user.Enabled = false;
user.Enabled1 = true;
}
users.Add(user);
}
//数据绑定
datagrid.ItemsSource = users;
}
//定义要绑定的类
private class User
{
public string Name { get; set; }
public string Sex { get; set; }
public string Age { get; set; }
public string Phone { get; set; }
public string BtnActionStr { get; set; }
public bool Enabled { get; set; }
public string BtnActionStr1 { get; set; }
public bool Enabled1 { get; set; }
}
//平均分配各列的宽度
private void datagrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
int WidthSize = (int)(datagrid.ActualWidth / 5 - 4);
UserName.Width = WidthSize; UserSex.Width = WidthSize; UserAge.Width = WidthSize;
UserPhone.Width = WidthSize; UserAction.Width = WidthSize;
}
//第一个按钮点击事件
private void BtnAction_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(users[datagrid.SelectedIndex].Name);
}
//第二个按钮点击事件
private void BtnAction1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(users[datagrid.SelectedIndex].Sex);
}
调用:
GetDataGrid();
效果图:



WPF中使用DataGrid时操作列按钮问题的更多相关文章
- wpf中的datagrid绑定操作按钮是否显示或者隐藏
如图,需要在wpf中的datagrid的操作那列有个确认按钮,然后在某些条件下确认按钮可见,某些情况下不可见的,放在mvc里直接在cshtml页面中if..else就行了. 但是在wpf里不行..网上 ...
- WPF 中获取DataGrid 模板列中控件的对像
WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...
- WPF中ListBox滚动时的缓动效果
原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...
- wpf中为DataGrid添加checkbox支持多选全选
项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...
- Wpf 中的DataGrid的Header属性,动态bind时不起作用
在使用wpf开发软件时,有使用到DataGrid,DataGridTextColumn的Header 属性使用DynamicResource binding,在修改绑定数据源时,header并没有更新 ...
- layui中table表格的操作列(删除,编辑)等按钮的操作
暂停和中止按钮功能 if (obj.event === 'del') { layer.confirm('确认中止么', function (index) { $.ajax({ type: " ...
- WPF中自定义标题栏时窗体最大化处理之WindowChrome
注意: 本文方法基础是WindowChrome,而WindowChrome在.NET Framework 4.5之后才集成发布的.见:WindowChrome Class 在.NET Framewor ...
- easyui datagrid自定义操作列
通过formatter方法给Jquery easyui 的datagrid 每行增加操作链接 我们都知道Jquery的EasyUI的datagrid可以添加并且自定义Toolbar, 这样我们选择一行 ...
- WPF中修改DataGrid单元格值并保存
编辑DataGrid中的单元格的内容然后保存是非常常用的功能.主要涉及到的方法就是DataGrid的CellEditEnding 和BeginningEdit .其中BeginningEdit 是当 ...
随机推荐
- Codeforces 628F 最大流转最小割
感觉和昨天写了的题一模一样... 这种题也能用hall定理取check, 感觉更最小割差不多. #include<bits/stdc++.h> #define LL long long # ...
- Scala模式匹配| 隐式转换
1. 模式匹配 Scala中的模式匹配类似于Java中的switch语法,但是更加强大.模式匹配语法中,采用match关键字声明,每个分支采用case关键字进行声明,当需要匹配时,会从第一个case分 ...
- Python requests--初识接口自动化
requests模块初级宝典:http://docs.python-requests.org/zh_CN/latest/user/quickstart.htmlrequests模块之葵花宝典:http ...
- KMP替代算法——字符串Hash
很久以前写的... 今天来谈谈一种用来替代KMP算法的奇葩算法--字符串Hash 例题:给你两个字符串p和s,求出p在s中出现的次数.(字符串长度小于等于1000000) 字符串的Hash 根据字面意 ...
- vue调用Moment显示时间
1.下载 Moment 网站: http://momentjs.cn/ 2创建一个vue的文本格式 admin.vue 3.定义给值 代码如下 <template> <div ...
- Nginx+Https自己敲命令生成证书
nginx配置https访问 一.准备 环境:centos6.8 nginx:1.13.6 二.开始 首先安装依赖包: yum install -y gcc gcc-c++ autocon ...
- 解决C#中FileSystemWatcher类的Changed事件触发多次的问题
public static void WatchFile() { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = ...
- Go并发示例-Pool
https://mp.weixin.qq.com/s/MBY6l5VxrFPJ4AA8nGeQUQ <Go语言实战>笔记(十六) | Go并发示例-Pool 飞雪无情 异步图书 2017- ...
- java web 读取数据库数据写入Excel返回浏览器下载
@RequestMapping(value = "/download", method = RequestMethod.GET) public void downstudents( ...
- Git最牛最全详解
阅读目录 Git是什么 SVN与Git的最主要的区别 在windows上如何安装Git 如何操作 创建版本库 把文件添加到版本库中 版本回退 理解工作区与暂存 ...