将DataGrid.RowDetailsTemplate的激活限制为某些列

时间:2013-06-07 16:37:58

标签: c# .net wpf datagrid rowdetailstemplate

拥有一个包含多列的DataGrid 想要将DataGrid.RowDetailsTemplate的激活限制为某些列 实际上我只想从激活DataGrid.RowDetailsTemplate中排除一列。

1 个答案:

答案 0 :(得分:1)

您可以使用RowDetailsVisibilityChanged事件(msdn),在事件处理程序中,您可以检查所选列并隐藏或显示行详细信息。

在下面的示例中,当我在“名称”列中选择单元格时,我不想显示行详细信息。

XAML:

<DataGrid Name="outerGrid" AutoGenerateColumns="False"                 
          ItemsSource="{Binding}"                  
          RowDetailsVisibilityChanged="outerGrid_RowDetailsVisibilityChanged" >
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <DataGrid Name="innerGrid" ItemsSource="{Binding Items}" AutoGenerateColumns="False" >
                <DataGrid.Columns>
                    <DataGridTextColumn  Header="ID" Binding="{Binding Id}"/>
                    <DataGridTextColumn  Header="Name" Binding="{Binding Name}"/>
                    <DataGridTextColumn  Header="Surname" Binding="{Binding Surname}"/>
                </DataGrid.Columns>
            </DataGrid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
    <DataGrid.Columns>
        <DataGridTextColumn  Header="ID" Binding="{Binding Id}"/>
        <DataGridTextColumn  Header="Name" Binding="{Binding Name}"/>
        <DataGridTextColumn  Header="Surname" Binding="{Binding Surname}"/>
    </DataGrid.Columns>
</DataGrid>

代码隐藏:

private void outerGrid_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
{
    DataGrid dg = sender as DataGrid;
    if (dg != null)
    {
        if (dg.CurrentCell != null && dg.CurrentCell.Column.Header.ToString() == "Name")
        {                    
            e.DetailsElement.Visibility = System.Windows.Visibility.Collapsed;
        }
        else
        {   
            e.DetailsElement.Visibility = System.Windows.Visibility.Visible;
        }
    }
}