我有一个 DataGrid,它的 RowDetailsTemplate 设置为 ListBox。我已将 DataGridRowHeaderStyle 设置为 ToggleButton,如下所示:
<Style x:Key="DataGridRowHeaderStyle" TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="36"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border x:Name="bd" BorderBrush="Black" BorderThickness="1,0.5,1,1" Height="36" Width="20" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Margin="0" >
<ToggleButton Style="{StaticResource RowDetailExpanderWithPlusSymbol}"
Visibility="{Binding Converter={StaticResource RowDetailsVisibilityConverter}}"
Click="ToggleButton_Click"
MouseDoubleClick="ToggleButton_MouseDoubleClick">
<!--<ToggleButton.CommandParameter>
<MultiBinding Converter="{StaticResource SelectedCodesMultiBindingConverter}">
<Binding Path="."/>
<Binding RelativeSource="{RelativeSource Self}" Path="IsChecked"/>
</MultiBinding>
</ToggleButton.CommandParameter>-->
<ToggleButton.IsChecked>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}" Path="DetailsVisibility" Mode="TwoWay" >
<Binding.Converter>
<infConv:DetailsVisibilityToBool FalseToVisibility="Collapsed" />
</Binding.Converter>
</Binding>
</ToggleButton.IsChecked>
</ToggleButton>
</Border>
<Border Grid.Row="1" VerticalAlignment="Stretch"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当我单击 DataGridRowHeader 切换按钮时,我正在选择 DataGridRow,如下所示:
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
try
{
ToggleButton tg = sender as ToggleButton;
UiUser user = tg.DataContext as UiUser;
DataGridRow dr = dg.ItemContainerGenerator.ContainerFromItem(user) as DataGridRow;
dr.IsSelected = true;
dr.Focus();
}
catch (Exception ex)
{
}
}
我想要的是下次我选择 ListBox 项目时,它应该被选中并处于活动状态。但是,我必须单击它两次才能使其处于活动状态。请帮助。
编辑 - RowDetailsTemplate:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<ContentControl DataContext="{Binding}">
<ListBox x:Name="lBox" ItemsSource="{Binding Similar}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Blue"/>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue"/>
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGray" />
</Style.Resources>
<!--<EventSetter Event="PreviewMouseDown" Handler="lBoxItem_PreviewMouseDown"/>-->
<EventSetter Event="MouseDoubleClick" Handler="LBox_MouseDoubleClick_Inside"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Blue"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Border Margin="-3,-3,0,0" BorderBrush="Gray" Width="{Binding ElementName=NameDataGridColumn, Path=ActualWidth}" BorderThickness="1,0,0,1">
<TextBlock ToolTip="{Binding person, Converter={StaticResource FormattedNameConverter}}" Margin="5,0,0,0" TextTrimming="CharacterEllipsis"
Text="{Binding InternalUser.person, Converter={StaticResource FormattedNameConverter}}" VerticalAlignment="Center">
</TextBlock>
</Border>
<Border Margin="0,0,0,0" BorderBrush="Gray" Width="{Binding ElementName=OrgDataGridColumn, Path=ActualWidth}" BorderThickness="1,0,0,1">
<TextBlock HorizontalAlignment="Stretch" TextTrimming="CharacterEllipsis" ToolTip="{Binding Path=organizationsString}"
Text="{Binding Path=organizationsString}"
Margin="5,0,0,0" VerticalAlignment="Center"/>
</Border>
<Border Margin="0,0,0,0" BorderBrush="Gray" Width="{Binding ElementName=RolesDataGridColumn, Path=ActualWidth}" BorderThickness="1,0,0,1">
<TextBlock HorizontalAlignment="Stretch" Margin="5,0,0,0"
Text="{Binding Path=rolesString}" ToolTip="{Binding Path=rolesString}"
TextTrimming="CharacterEllipsis" VerticalAlignment="Center"
ToolTipService.Placement="Bottom" />
</Border>
<Border Margin="0,0,0,0" BorderBrush="Gray" Width="{Binding ElementName=NPIDDataGridColumn, Path=ActualWidth}" BorderThickness="1,0,0,1">
<TextBlock HorizontalAlignment="Stretch"
Text="{Binding InternalUser.npid}" ToolTip="{Binding InternalUser.npid}" TextTrimming="CharacterEllipsis" Margin="5,0,0,0" VerticalAlignment="Center"/>
</Border>
<Border Margin="0,0,0,0" BorderBrush="Gray" Width="{Binding ElementName=EmailDataGridColumn, Path=ActualWidth}" BorderThickness="1,0,0,1">
<TextBlock Text="{Binding InternalUser.principal.user_name}" ToolTip="{Binding InternalUser.principal.user_name}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"/>
</Border>
<Border Margin="0,0,0,0" BorderBrush="Gray" Width="{Binding ElementName=Status, Path=ActualWidth}" BorderThickness="1,0,0,1">
<TextBlock Text="{Binding ActiveStatus}" ToolTip="{Binding ActiveStatus}" Padding="4,0,0,0" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" />
</Border>
<Border Margin="0,0,0,0" BorderBrush="Gray" Width="{Binding ElementName=DMUserName, Path=ActualWidth}" BorderThickness="1,0,0,1">
<TextBlock Text="{Binding InternalUser.directMailAccountId}"
VerticalAlignment="Center" TextTrimming="CharacterEllipsis"
ToolTip="{Binding InternalUser.directMailAccountId}"/>
</Border>
<Border Margin="0,0,0,0" BorderBrush="Gray" Width="{Binding ElementName=DMAccountStatus, Path=ActualWidth}" BorderThickness="1,0,0,1">
<TextBlock Margin="5,0,0,0" Text="{Binding DMStatus}"
HorizontalAlignment="Stretch" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"
ToolTip="{Binding DMStatus}"/>
</Border>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ContentControl>
</DataTemplate>
</DataGrid.RowDetailsTemplate>