绑定到属性的WPF Datagrid RowDetailsTemplate可见性 [英] WPF Datagrid RowDetailsTemplate visibility bound to a property
问题描述
< dg:DataGrid x:Name =MyGrid
ItemsSource ={Binding Path = MyItems}
AutoGenerateColumns = True
SelectionMode =扩展
RowDetailsVisibilityMode =VisibleWhenSelected>
< dg:DataGrid.RowDetailsTemplate>
< DataTemplate>
< TextBlock Text =更多详细信息.../>
< / DataTemplate>
< / dg:DataGrid.RowDetailsTemplate>
...
< / dg:DataGrid>
不幸的是,对于此应用程序,在选定行上显示行详细信息并不直观,客户端想要点击多行的复选框来显示RowDetails窗格,但也可以围绕选择其他行的网格滚动。换句话说,修复显示RowDetails的行,无论DataGrid发生什么。
所以当前滚动关闭它们已经打开的RowDetailsPane。我想做的是在其中一个列中添加一个复选框,并将RowDetails面板的可见性绑定到此属性,但我无法弄清楚如何执行此操作。问题只在于RowDetailsPane只能在数据网格中的行选择上进行操作 - 可以通过某种方式扩展以对我选择的属性进行操作吗?
提前感谢
将
在WPF工具包源代码中,每个DataGridRow都有一个DetailsVisibility属性。
我在第一列中放了一个按钮(仅用于测试)。
< toolkit:DataGridTemplateColumn>
< toolkit:DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Button x:Name =buttonDetailsContent =HelloButtonBase.Click =Details_Click/>
< / DataTemplate>
< / toolkit:DataGridTemplateColumn.CellTemplate>
< / toolkit:DataGridTemplateColumn>
当点击按钮时,找到点击的行并切换属性。
private void Details_Click(object sender,RoutedEventArgs e)
{
try
{
//原始来源是什么被点击。例如
//一个按钮。
DependencyObject dep =(DependencyObject)e.OriginalSource;
//向上迭代遍历视觉树,寻找
//点击的行。
while((dep!= null)&&(dep是DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
//如果我们发现点击的行
if(dep!= null&& dep是DataGridRow)
{
//获取行
DataGridRow row =(DataGridRow)dep;
//更改详细信息可见性
if(row.DetailsVisibility == Visibility.Collapsed)
{
row.DetailsVisibility = Visibility.Visible;
}
else
{
row.DetailsVisibility = Visibility.Collapsed;
}
}
}
catch(System.Exception)
{
}
}
我没有通过数据绑定来探索。
I am using a WPF Datagrid with a RowDetails panel where the RowDetailsVisibilityMode is set to "VisibleWhenSelected" and the SelectionMode="Extended" so that multiple rows can be selected and hence display RowDetails, as below:
<dg:DataGrid x:Name="MyGrid"
ItemsSource="{Binding Path=MyItems}"
AutoGenerateColumns="True"
SelectionMode="Extended"
RowDetailsVisibilityMode="VisibleWhenSelected">
<dg:DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="Further Details..."/>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
...
</dg:DataGrid>
Unfortunately, for this application it isn't intuitive to display row details on 'selected' rows, the client would like to click a checkbox on a number of rows to display the RowDetails pane, but also scroll around the grid selecting other rows. In other words fix the rows that display RowDetails no matter what happens on the DataGrid.
So currently scrolling around closes the RowDetailsPanes that they have opened. What I would like to do is to have a checkbox in one of the columns and bind the RowDetails panel visibility to this property but I can't figure out how to do it. The problem is simply that RowDetailsPane only operates on the row selection(s) in the datagrid - can it be extended somehow to operate on a property of my choosing?
Thanks in advance, Will
Looking at the WPF toolkit source code each DataGridRow has a DetailsVisibility property.
I put a button (just for testing) in the first column.
<toolkit:DataGridTemplateColumn>
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="buttonDetails" Content="Hello" ButtonBase.Click="Details_Click" />
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
When the button is clicked, find the clicked row and toggle the property.
private void Details_Click(object sender, RoutedEventArgs e)
{
try
{
// the original source is what was clicked. For example
// a button.
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree upwards looking for
// the clicked row.
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
// if we found the clicked row
if (dep != null && dep is DataGridRow)
{
// get the row
DataGridRow row = (DataGridRow)dep;
// change the details visibility
if (row.DetailsVisibility == Visibility.Collapsed)
{
row.DetailsVisibility = Visibility.Visible;
}
else
{
row.DetailsVisibility = Visibility.Collapsed;
}
}
}
catch (System.Exception)
{
}
}
I have not explored doing this via databinding.
这篇关于绑定到属性的WPF Datagrid RowDetailsTemplate可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!