从显示RowDetails中排除DataGrid列

时间:2014-08-13 20:20:14

标签: c# wpf datagrid datagridtemplatecolumn rowdetails

我正在通过编写一个小工具来解析CME XML配置文件(如current Production configuration)来自学WPF,LINQ和MVVM。我已经向DataGrid添加了一个带有复选框的DataGridTemplateColumn,用于显示频道,以便我可以选择一些我感兴趣的频道并过滤掉其余频道。问题是当我单击复选框时,会显示RowDetails。

如何在仍然拥有" VisibleWhenSelected"时,如何阻止点击触发RowDetails的显示?单击任何其他列时的行为?我是否需要自己处理RowDetails的可见性,还是有更简单的方法?

以下是根据以下建议添加ToggleButton的更新代码,以防万一对其他人有用:

<DataGrid x:Name="ChannelListGrid"
          DockPanel.Dock="Top"
          IsReadOnly="True"
          AutoGenerateColumns="False"
          ItemsSource="{Binding Path=ConfigFileXML.Root.Elements[channel]}"
          RowDetailsTemplate="{StaticResource ResourceKey=ConnectionInfoTemplate}"
          IsTextSearchEnabled="True"
          HorizontalAlignment="Left"
          AreRowDetailsFrozen="True"
          RowDetailsVisibilityMode="Collapsed">
    <DataGrid.Columns>
        <DataGridTemplateColumn Width="Auto"
                                CanUserResize="False"
                                CanUserSort="false"
                                CanUserReorder="False">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ToggleButton Click="ToggleRowDetails"
                                  Style="{StaticResource ToggleExpandButtonStyle}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Selected"
                                Width="Auto"
                                CanUserReorder="False"
                                CanUserResize="False">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Unchecked="ChannelDeselected"
                              Checked="ChannelSelected" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="ID"
                            Binding="{Binding Path=Attribute[id].Value}" />
        <DataGridTextColumn Header="Name"
                            Binding="{Binding Path=Attribute[label].Value}" />
        <DataGridTextColumn Header="Symbol Count"
                            Binding="{Binding Path=Descendants[product].Count}" />
    </DataGrid.Columns>
</DataGrid>

以下是模板

<DataTemplate x:Key="ConnectionInfoTemplate">
    <DataGrid x:Name="ConnectionListGrid"
              IsReadOnly="True"
              HorizontalAlignment="Left"
              AutoGenerateColumns="False"
              ItemsSource="{Binding Path=Descendants[connection]}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID"
                                Binding="{Binding Path=Attribute[id].Value}" />
            <DataGridTextColumn Header="Type"
                                Binding="{Binding Path=Element[type].Value}" />
            <DataGridTextColumn Header="Protocol"
                                Binding="{Binding Path=Element[protocol].Value}" />
            <DataGridTextColumn Header="Source IP"
                                Binding="{Binding Path=Element[ip].Value}" />
        </DataGrid.Columns>
    </DataGrid>
</DataTemplate>
<SolidColorBrush x:Key="GlyphBrush"
                 Color="#444" />

<Style x:Key="ToggleExpandButtonStyle"
       TargetType="{x:Type ToggleButton}">
    <Setter Property="IsChecked"
            Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Grid Width="15"
                      Height="13"
                      Background="Transparent">
                    <Path x:Name="ExpandPath"
                          HorizontalAlignment="Left"
                          VerticalAlignment="Center"
                          Margin="1,1,1,1"
                          Fill="{StaticResource GlyphBrush}"
                          Data="M 4 0 L 8 4 L 4 8 Z" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked"
                             Value="True">
                        <Setter Property="Data"
                                TargetName="ExpandPath"
                                Value="M 0 4 L 8 4 L 4 8 Z" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是C#

背后的代码
private T FindAncestor<T>(DependencyObject depObject)
    where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(depObject);
    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindAncestor<T>(parent);
}

private void ToggleRowDetails(object sender, System.Windows.RoutedEventArgs e)
{
    var senderButton = sender as ToggleButton;
    DataGridRow toggledRow = FindAncestor<DataGridRow>(senderButton);

    if (toggledRow != null)
    {
        // If IsChecked is null, use false. If true, make details visible, otherwise collapse the details
        toggledRow.DetailsVisibility = ( senderButton.IsChecked ?? false) ? Visibility.Visible : Visibility.Collapsed;
    }
}

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,并决定添加一个切换按钮列来手动显示行详细信息。

风格

<SolidColorBrush x:Key="GlyphBrush" Color="#444" />

<Style x:Key="ToggleExpandButtonStyle" TargetType="{x:Type ToggleButton}">
    <Setter Property="IsChecked" Value="False"/>
    <Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
            <Grid Width="15" Height="13" Background="Transparent">
                <Path x:Name="ExpandPath" HorizontalAlignment="Left" VerticalAlignment="Center" 
                          Margin="1,1,1,1" Fill="{StaticResource GlyphBrush}" Data="M 4 0 L 8 4 L 4 8 Z"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Data" TargetName="ExpandPath" Value="M 0 4 L 8 4 L 4 8 Z"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
    </Setter>
</Style>

Datagrid列

            <DataGridTemplateColumn Width="Auto" CanUserResize="False" CanUserSort="false" CanUserReorder="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                    <ToggleButton Click="ToggleButton_Click" Style="{StaticResource ToggleExpandButtonStyle}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

点击事件(在VB中)

    Public Shared Sub ToggleButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    Dim senderButton As Primitives.ToggleButton
    senderButton = CType(sender, Primitives.ToggleButton)
    Dim clickedRow As DataGridRow = CType(GetVisualParent(senderButton, GetType(DataGridRow)), DataGridRow)

    If clickedRow IsNot Nothing Then
        If senderButton.IsChecked Then
            clickedRow.DetailsVisibility = Windows.Visibility.Visible
            clickedRow.BringIntoView()
        Else
            clickedRow.DetailsVisibility = Windows.Visibility.Collapsed
        End If
    End If
  End Sub