首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >排除DataGrid列显示RowDetails

排除DataGrid列显示RowDetails
EN

Stack Overflow用户
提问于 2014-08-13 20:20:13
回答 1查看 371关注 0票数 0

我正在教自己WPF、LINQ和MVVM,方法是编写一个小工具来解析CME配置文件,比如当前生产配置。我将一个带有复选框的DataGridTemplateColumn添加到显示通道的DataGrid中,这样我就可以选择我感兴趣的几个频道,并过滤掉其余的通道。问题是,当我单击复选框时,就会显示RowDetails。

如果单击了其他列,如何防止单击触发RowDetails的显示,同时仍然具有"VisibleWhenSelected“行为?我是否需要亲自处理RowDetails的可见性,还是有更简单的方法?

这里是更新的代码,ToggleButton按照下面的建议添加,以防对其他人有用:

代码语言:javascript
复制
<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>

以下是模板

代码语言:javascript
复制
<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#中的代码

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-13 20:38:31

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

风格

代码语言:javascript
复制
<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>

数据柱

代码语言:javascript
复制
            <DataGridTemplateColumn Width="Auto" CanUserResize="False" CanUserSort="false" CanUserReorder="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                    <ToggleButton Click="ToggleButton_Click" Style="{StaticResource ToggleExpandButtonStyle}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

单击事件(在VB中)

代码语言:javascript
复制
    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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25295383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档