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

Posted

技术标签:

【中文标题】如何将 SelectedItem 从其子项设置为 ListViewItem【英文标题】:How to set SelectedItem to ListViewItem from its child 【发布时间】:2021-08-12 04:08:16 【问题描述】:

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

附加信息.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>

如此处How can I get access to ListViewItem from its child control? 所示,可以使用代码隐藏设置SelectedItem,但我不喜欢在此简单任务中使用代码隐藏。

附加信息.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>

附加信息.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];

【问题讨论】:

【参考方案1】:

您可以使用Trigger 来设置IsSelected 状态,具体取决于您的项目容器样式中的IsKeyboardFocusWithin。每当您关注 TextBox 时,它也会关注该行。

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

这是与您的项目容器样式合并的触发器。请注意,SelectedItem 只会在您的 TextBox 被聚焦时被设置,因为您在自己的风格中将 Focusable 设置为 False。当焦点移动时,例如单击按钮,SelectedItem 将重置为null。如果您删除了可聚焦的设置器,触发器仍然有效,该行保持焦点,即使单击按钮,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>

【讨论】:

这正是我想要的——谢谢!!

以上是关于如何将 SelectedItem 从其子项设置为 ListViewItem的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin 表单设置 Picker SelectedItem

您如何以编程方式将焦点设置到已具有焦点的 WPF ListBox 中的 SelectedItem?

Xamarin Forms StackLayout:如何将子项的宽度/高度设置为百分比

如何将 QTreeWidget 的子项设置为在单击其父项时自动选择并突出显示

如何从其构造函数设置自定义异常类的 InnerException

Swiftui 将 VStack 子项高度调整为内容高度