【问题标题】:Set focus on the selected item when tabbing in a WPF ListBox在 WPF 列表框中切换时将焦点设置在所选项目上
【发布时间】:2016-12-30 21:15:27
【问题描述】:

我的 xaml 文件中有一个 ListBox,如下所示:

<ListBox Name="collateralPledgedListbox" 
         Style="{StaticResource USB_ListBox}" 
         Margin="0,0,5,39" Width="Auto"  
         ItemsPanel="{StaticResource USB_ListBox_ItemPlacement}"  
         TabNavigation="Local"  
         ItemContainerStyle="{StaticResource USB_ListBoxContainer}"  
         ItemsSource="{Binding Model.CollateralPledgedByMarriedIndividuals, Mode=TwoWay}"  
         ItemTemplate="{StaticResource USBcollateralColSumColInfoDataTemplate}"  
         ScrollViewer.HorizontalScrollBarVisibility="Hidden"> 
</ListBox>

当行数超过提供的空间时,此 ListBox 会显示一个垂直滚动条。 在 ListBox 中的控件之间切换时,我无法向用户显示当前选定的行。

我使用 C# 作为我的语言。对此的任何帮助将不胜感激。

【问题讨论】:

    标签: c# wpf model-view-controller listbox


    【解决方案1】:

    听起来您所要做的就是集中注意力并刷新视图。 您是否尝试过按照this 回答的建议进行操作?

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      MainListBox.SelectedItem = MainListBox.Items[3];
      MainListBox.UpdateLayout(); // Pre-generates item containers 
    
     var listBoxItem = (ListBoxItem) MainListBox
          .ItemContainerGenerator
          .ContainerFromItem(MainListBox.SelectedItem);
    
      listBoxItem.Focus();
    }
    

    【讨论】:

    • 感谢 Noctis 的回复,但由于我不知道所选项目的索引,因此此解决方案不起作用。
    • 你能概括一下你的问题吗?我不确定是什么/为什么是问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 2012-07-29
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多