在使用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时操作列按钮问题的更多相关文章

  1. wpf中的datagrid绑定操作按钮是否显示或者隐藏

    如图,需要在wpf中的datagrid的操作那列有个确认按钮,然后在某些条件下确认按钮可见,某些情况下不可见的,放在mvc里直接在cshtml页面中if..else就行了. 但是在wpf里不行..网上 ...

  2. WPF 中获取DataGrid 模板列中控件的对像

    WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...

  3. WPF中ListBox滚动时的缓动效果

    原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...

  4. wpf中为DataGrid添加checkbox支持多选全选

    项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...

  5. Wpf 中的DataGrid的Header属性,动态bind时不起作用

    在使用wpf开发软件时,有使用到DataGrid,DataGridTextColumn的Header 属性使用DynamicResource binding,在修改绑定数据源时,header并没有更新 ...

  6. layui中table表格的操作列(删除,编辑)等按钮的操作

    暂停和中止按钮功能 if (obj.event === 'del') { layer.confirm('确认中止么', function (index) { $.ajax({ type: " ...

  7. WPF中自定义标题栏时窗体最大化处理之WindowChrome

    注意: 本文方法基础是WindowChrome,而WindowChrome在.NET Framework 4.5之后才集成发布的.见:WindowChrome Class 在.NET Framewor ...

  8. easyui datagrid自定义操作列

    通过formatter方法给Jquery easyui 的datagrid 每行增加操作链接 我们都知道Jquery的EasyUI的datagrid可以添加并且自定义Toolbar, 这样我们选择一行 ...

  9. WPF中修改DataGrid单元格值并保存

    编辑DataGrid中的单元格的内容然后保存是非常常用的功能.主要涉及到的方法就是DataGrid的CellEditEnding  和BeginningEdit .其中BeginningEdit 是当 ...

随机推荐

  1. ACA:利用ACA解决TSP优化最佳路径问题——Jason niu

    load citys_data.mat n = size(citys,1); D = zeros(n,n); for i = 1:n for j = 1:n if i ~= j D(i,j) = sq ...

  2. 20172328 2018-2019《Java软件结构与数据结构》第八周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第八周学习总结 概述 Generalization 本周学习了二叉树的另一种有序扩展?是什么呢?你猜对了!ヾ(◍°∇°◍) ...

  3. PostgreSQL自学笔记:3 数据库的基本操作

    3 数据库的基本操作 3.1 创建数据库 3.1.1 使用对象浏览器创建数据库 [Server] -> PostgreSQL 9.6 -> 数据库,右击 -> 创建 通常: 数据库: ...

  4. css Margin-top塌陷,解决方法

    在两个盒子嵌套时,内部的盒子设置的margin-top会加到外边的盒子上,导致内部的盒子margin-top设置失败,解决方法如下: (1)外部盒子设置一个边框 (2)外部盒子设置overflow:h ...

  5. jquery复制图片

    <div class="img-div">           <a href="javascript:void(0);"><im ...

  6. 微信公众号_订阅号_access_token_创建菜单_菜单name+表情

    全局唯一接口调用凭据 access_token 用于接口调用的一个必要参数 有了 access_token 就能实现所有的接口 特点:  1. 有效期为 2 小时,所以 2 小时要更新一次,提前 5 ...

  7. 11. cookie_session_原生ajax_readyState的值_同源策略_跨域_jsonp的使用

    1. cookie 浏览器存储技术.(服务器将少量数据交于浏览器存储管理) 作用: 存储数据, 解决 http 协议无状态问题 工作流程: 浏览器发送请求给服务器,请求登录 服务器返回响应给浏览器,此 ...

  8. PHP使用 strpos() 注意事项

    返回字符出现的第一个位置, 如果字符在被搜索字符串的开头, 则会返回 ‘0’ 因此, 在使用此函数判断 字符串是否包含 某一个字符时  使用: if(strpos('string','str') != ...

  9. scrapy入门使用

    scrapy入门 创建一个scrapy项目 scrapy startporject mySpider 生产一个爬虫 scrapy genspider itcast "itcast.cn&qu ...

  10. 微信小程序底部tabbar

    在 app.json    文件里面 : { "pages":[ "pages/index/index", "pages/logs/logs" ...