0

我有一个 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>
4

1 回答 1

0

我遇到了同样的问题:有一个 DataGrid,每行都有一个 RowDetails - RowDetailsTemplate 由两个 ListBoxes 组成。ListBoxItem 是一个切换按钮。所以,我想通过第一次点击来选择切换按钮。首先,我创建了这个http://wpf.codeplex.com/wikipage?title=Single-Click%20Editing。所以,我更新了我的 ListBox 的样式

 <Style x:Key="MyListBoxStyle" BasedOn="{StaticResource ToggleButtonListBoxStyle}" TargetType="{x:Type ListBox}">
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBoxItem_PreviewMouseLeftButtonDown"></EventSetter>
                </Style>
            </Setter.Value>
        </Setter>

在 CodeBehind 中:

private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var item = sender as ListBoxItem;
        if (item != null)
        {
            var toggleButton = WpfUiHelper.FindVisualChild<ToggleButton>(item);

            if (toggleButton != null)
            {
                if (toggleButton.IsChecked == false) toggleButton.IsChecked = true;
                else toggleButton.IsChecked = false;
                e.Handled = true;
            }
        }
    }

现在,当我在最合适的时间单击 RowDetails 中的切换按钮时 - 它会选择或取消选择。

希望它会有所帮助。

于 2014-01-09T08:24:02.753 回答