0

当通过导航获得焦点时,是否有一种简单的方法来设置SelectedItem我的?ListViewTextBoxTab

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

如此处所示如何从子控件访问 ListViewItem?可以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];
}
4

1 回答 1

0

您可以使用 a根据您的项目容器样式Trigger设置状态。每当您关注 a时,它也会关注该行。IsSelectedIsKeyboardFocusWithinTextBox

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

这是与您的项目容器样式合并的触发器。请注意,SelectedItem只有当您TextBox专注时才会设置,因为您将 设置FocusableFalse您的风格。当焦点移动时,例如单击按钮,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>
于 2021-05-25T09:40:07.583 回答