如何将 SelectedItem 从其子项设置为 ListViewItem

How to set SelectedItem to ListViewItem from its child

TextBox 通过 Tab 导航获得焦点时,有没有简单的方法来设置我的 ListViewSelectedItem

AdditionalInformation.xaml:

<ListView Grid.Row="2" ItemsSource="{Binding PropertieList}" Style="{DynamicResource AdditionalInfo_ListViewStyle}" >
    <ListView.View>
        <GridView ColumnHeaderContainerStyle="{DynamicResource AdditionalInfo_GridView_HeaderStyle}">
            <GridViewColumn Header=" Name" KeyboardNavigation.TabNavigation="None" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" Style="{DynamicResource AdditionalInfo_Body_TextBlockStyle}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header=" Value"  >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Value}" IsEnabled="{Binding AllowValueChanging}" MinWidth="300" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

资源词典

<Style x:Key="AdditionalInfo_ListViewStyle"  TargetType="{x:Type ListView}">
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Stretch" />
    <Setter Property="Background" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush}}" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Margin" Value="14,0,14,0" />
    <Setter Property="SelectionMode" Value="Single" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue" />
    
    <!--Row style-->
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="VerticalContentAlignment" Value="Top" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Focusable" Value="false"/>
                <Setter Property="Background" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush}}" />
                <Style.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="true"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush_Hover}}"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush_Hover}}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush_IsSelected}}"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush_IsSelected}}"/>
                    </MultiTrigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

<!--Header-->
<Style x:Key="AdditionalInfo_GridView_HeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="Visibility" Value="Visible" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Header_ForegrounddBrush_enabled}}" />
    <Setter Property="Background" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Header_BackgroundBrush}}" />
</Style>

<Style x:Key="AdditionalInfo_Body_TextBlockStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsEnabled" Value="true"/>
            </MultiTrigger.Conditions>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_ForegrounddBrush_enabled}}" />
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsEnabled" Value="false"/>
            </MultiTrigger.Conditions>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_ForegrounddBrush_disabled}}" />
        </MultiTrigger>
    </Style.Triggers>
</Style>

如此处所示 可以通过使用代码隐藏来设置 SelectedItem 但我不喜欢为此使用代码隐藏 简单任务。

AdditionalInformation.xaml:

<ListView x:Name="AdditionalInfoListView" Grid.Row="2" ItemsSource="{Binding PropertieList}" Style="{DynamicResource AdditionalInfo_ListViewStyle}" >
    <ListView.View>
        <GridView ColumnHeaderContainerStyle="{DynamicResource AdditionalInfo_GridView_HeaderStyle}">
            <GridViewColumn Header=" Name" KeyboardNavigation.TabNavigation="None" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" Style="{DynamicResource AdditionalInfo_Body_TextBlockStyle}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header=" Value"  >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Value}" IsEnabled="{Binding AllowValueChanging}" GotFocus="TextBox_GotFocus" MinWidth="300" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

AdditionalInformation.xaml.cs

private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
    var item = (sender as TextBox).DataContext;
    int index = AdditionalInfoListView.Items.IndexOf(item);
    AdditionalInfoListView.SelectedItem = AdditionalInfoListView.Items[index];
}

您可以使用 Trigger 根据项目容器样式中的 IsKeyboardFocusWithin 设置 IsSelected 状态。每当您聚焦 TextBox 时,它也会聚焦该行。

<Trigger Property="IsKeyboardFocusWithin" Value="True">
   <Setter Property="IsSelected" Value="True"/>
</Trigger>

这是与您的项目容器样式合并的触发器。请注意 SelectedItem 只会在您的 TextBox 聚焦时设置,因为您在您的样式中将 Focusable 设置为 False。当焦点移动时,例如单击一个按钮,SelectedItem 将重置为 null。如果您删除可聚焦 setter,触发器仍然有效,该行保持聚焦,即使单击按钮,SelectedItem 也会保留。

<Setter Property="ItemContainerStyle">
    <Setter.Value>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="VerticalContentAlignment" Value="Top" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Focusable" Value="false"/>
            <Setter Property="Background" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush}}" />
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="IsSelected" Value="True"/>
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsMouseOver" Value="true"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush_Hover}}"/>
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush_Hover}}"/>
                </MultiTrigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected" Value="true"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush_IsSelected}}"/>
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static local:ResourceKeys.ListView_L2_Background_Body_BackgroundBrush_IsSelected}}"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Setter.Value>
</Setter>