我正在教自己WPF、LINQ和MVVM,方法是编写一个小工具来解析CME配置文件,比如当前生产配置。我将一个带有复选框的DataGridTemplateColumn添加到显示通道的DataGrid中,这样我就可以选择我感兴趣的几个频道,并过滤掉其余的通道。问题是,当我单击复选框时,就会显示RowDetails。
如果单击了其他列,如何防止单击触发RowDetails的显示,同时仍然具有"VisibleWhenSelected“行为?我是否需要亲自处理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;
}
}发布于 2014-08-13 20:38:31
我有一个类似的问题,并决定添加一个切换按钮列,以手动显示行详细信息。
风格
<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>数据柱
<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 Subhttps://stackoverflow.com/questions/25295383
复制相似问题