wpf 我需要 RowDetailsTemplate 的展开/折叠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3829137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I need the Expand / Collapse for RowDetailsTemplate
提问by Mediator
I have a DataGrid
. It has DataGrid.RowDetailsTemplate
. When a button is clicked it should Expand / Collapse; how would I do that?
我有一个DataGrid
. 它有DataGrid.RowDetailsTemplate
。当一个按钮被点击时,它应该展开/折叠;我该怎么做?
<Custom:DataGrid RowDetailsVisibilityMode="VisibleWhenSelected" SelectionMode="Extended" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" CanUserSortColumns="False">
<Custom:DataGrid.RowDetailsTemplate>
<DataTemplate>
<Custom:DataGrid>
<Custom:DataGrid.Columns>
<Custom:DataGridTextColumn Binding="{Binding idClient, Mode=Default}" Header="Ид" IsReadOnly="True"/>
<Custom:DataGridTextColumn Binding="{Binding name_client, Mode=Default}" Header="Имя" IsReadOnly="True"/>
</Custom:DataGrid.Columns>
</Custom:DataGrid>
</DataTemplate>
</Custom:DataGrid.RowDetailsTemplate>
<Custom:DataGrid.Columns>
<Custom:DataGridTemplateColumn>
<Custom:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Expander IsExpanded="True"/>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellTemplate>
</Custom:DataGridTemplateColumn>
<Custom:DataGridTextColumn Binding="{Binding idPartner, Mode=Default}" Header="Ид" IsReadOnly="True"/>
<Custom:DataGridTextColumn Binding="{Binding name_partner, Mode=Default}" Header="Имя" IsReadOnly="True"/>
</Custom:DataGrid.Columns>
</Custom:DataGrid>
回答by penjepitkertasku
Check this ...
检查这个...
OR
或者
XAML :
XAML:
<DataGrid Name="dg1" AutoGenerateColumns="False" SelectionMode="Single" CanUserAddRows="false" CanUserDeleteRows="False" SelectionUnit="FullRow" >
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<Expander Expanded="Expander_Expanded" Collapsed="Expander_Collapsed">
</Expander>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" IsReadOnly="True" Width="100" Binding="{Binding Name}" />
<DataGridTextColumn Header="Title" IsReadOnly="True" Width="100" Binding="{Binding Title}" />
<DataGridTextColumn Header="Job" IsReadOnly="True" Width="100" Binding="{Binding Job}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Isi, Converter={StaticResource ResourceKey=isiTextConverter}}" Margin="10,5,0,0" />
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
MainWindow.xaml.cs
主窗口.xaml.cs
private void Expander_Expanded(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;
}
}
private void Expander_Collapsed(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;
}
}
Output
输出
回答by Bindya
Include Collapsed and expanded events as below
包括折叠和展开的事件如下
<Custom:DataGridTemplateColumn>
<Custom:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Expander Collapsed="exp_Collapsed" Expanded="exp_Expanded"/>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellTemplate>
</Custom:DataGridTemplateColumn>
In the code behind
在后面的代码中
private void exp_Collapsed(object sender, RoutedEventArgs e)
{
this.dataGrid1.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Collapsed;
}
private void exp_Expanded(object sender, RoutedEventArgs e)
{
this.dataGrid1.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
}
回答by Palash Roy
I have improved the previous answer :
我已经改进了以前的答案:
Instead of using DataGrid.RowHeaderTemplate use DataGridTemplateColumn as below:
而不是使用 DataGrid.RowHeaderTemplate 使用 DataGridTemplateColumn 如下:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Expander Expanded="Expander_OnExpanded" Collapsed="Expander_OnCollapsed">
</Expander>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Benifit is you need not to re position your mouse after clicking expander button.
好处是您无需在单击扩展器按钮后重新定位鼠标。
回答by ΩmegaMan
Instead of walking the tree to find the datagrid row from the expander, use the static method GetRowContainingElement
which is found on DataGridRow
. Such as:
不用遍历树从扩展器中找到数据网格行,而是使用GetRowContainingElement
在DataGridRow
. 如:
private void Expander_Process(object sender, RoutedEventArgs e)
{
if (sender is Expander expander)
{
var row = DataGridRow.GetRowContainingElement(expander);
row.DetailsVisibility = expander.IsExpanded ? Visibility.Visible
: Visibility.Collapsed;
}
}
Then the expander has only one method call for each of the events:
然后扩展器对每个事件只有一个方法调用:
<Expander Expanded="Expander_Process" Collapsed="Expander_Process" />
回答by Paul Lydon
Selecting a Row on the Grid should expand the Row using the RowDetailsTemplate to display the content. This makes that Row the Selected Row and sets the value of the DataGrid's SelectedIndex Property.
在网格上选择一行应该使用 RowDetailsTemplate 展开该行以显示内容。这使该行成为选定行并设置 DataGrid 的 SelectedIndex 属性的值。
To collapse the Row set the DataGrid's SelectedIndex Property to -1.
要折叠行,请将 DataGrid 的 SelectedIndex 属性设置为 -1。