本文介绍了向WPF DataGrid添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在WPF中创建一个 DataGrid
控件,其中每行的第一个单元格中都有一个按钮。单击此按钮将显示 RowDetailsTemplate
或SubRow。我的关注是如何添加一个按钮,显示/隐藏 RowDetailsTemplate
?
I want to create a DataGrid
control in WPF in which there is a button in the first cell of each row. Clicking this button will show RowDetailsTemplate
or SubRow. My concern is How do i add a button which will show/Hide the RowDetailsTemplate
?
任何帮助将会非常大赞赏
Any help will be greatly appreciated
感谢
推荐答案
首先创建一个DataGridTemplateColumn以包含按钮:
First create a DataGridTemplateColumn to contain the button:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="ShowHideDetails">Details</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
当按钮被点击时,更新包含DataGridRow的DetailsVisibility:
When the button is clicked, update the containing DataGridRow's DetailsVisibility:
void ShowHideDetails(object sender, RoutedEventArgs e)
{
for(var vis=sender as Visual; vis!=null; vis = VisualTreeHelper.GetParent(vis) as Visual)
if(vis is DataGridRow)
{
var row = (DataGridRow)vis;
row.DetailsVisibility =
row.DetailsVisibility==Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
break;
}
}
这篇关于向WPF DataGrid添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!