如何在 WPF 中的用户控件上修复集合控件中的选定项目(使用 MVVM 实现)

发布于 2024-08-17 19:32:52 字数 533 浏览 8 评论 0原文

我希望有人可以帮助我...

Josh Smith 写了一篇关于“使用模型-视图-视图-模型的 WPF 应用程序”的精彩文章,他的文章中包含以下内容 代码示例

如果您下载代码示例并运行应用程序并查看所有客户,然后选择一家公司(例如第四家公司),然后单击“创建新客户”(这将打开新客户的选项卡),然后单击返回“所有客户”选项卡,然后使用键盘尝试将所选项目向上移动一位到当前所选项目正上方的项目,但事实并非如此!相反,选择器再次从顶部开始。

我不确定为什么会发生这种情况,但我希望当您单击向上时,它会上升一项,而不是从列表顶部开始。我怀疑这与 FocusManager 有关,但我不确定。

有谁知道为什么控件会以这种方式运行?是否有可能,我应该采取什么方法来修改此代码并使其不“重置”所选项目?

我已经实现了一个基于此模板的项目,出于功能原因,我需要让键盘向上移动选择器并向下移动选择器。下来而不重置。

I hope someone can help me...

Josh Smith did a great article on "WPF apps with the Model-View View-Model", and included in his article was the following code sample.

If you download the code sample and run the app and view all customers, then select a company (e.g. 4th company), then click "Create new customer" (which will open a tab of the new customer), and then click back on the "All Customers" tab, and then using the keyboard try and move the selected item up one to the item directly over the current selected item, it doesn't! Instead the selector starts at the top again.

I am not sure why this happens, but I want it so that when you click up, it goes one item up, rather than starting at the top of the list. I suspect this has to do with FocusManager, but am not sure.

Does anyone know why the control behaves in this manner? Is it possible, and what approach I should take to modify this code and get it to not "reset" the selected item?

I have implemented a project based off this template and for functionality reasons I need to get the keyboard to move the selector up & down without it resetting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七色彩虹 2024-08-24 19:32:52

在您提供的示例中,设置 IsSelected 属性不会影响逻辑焦点,因此默认设置焦点。我目前想到的解决方法是强制关注代码隐藏中的元素。作为示例,将处理程序添加到列表视图的选择更改中,如下所示:

  private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    if (e.AddedItems.Count == 0 || e.RemovedItems.Count > 0) return;
    var item = (CustomerViewModel) e.AddedItems[0];
    var container = (UIElement) listView1.ItemContainerGenerator.ContainerFromItem(item);
    container.Focus();
  }

in the example you've provided setting IsSelected property does not affect the logical focus so the focus is set by default. Workaround I have in mind currently is to force focus for the element in codebehind. As an example add handler to selectionchanged of the listview like this:

  private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    if (e.AddedItems.Count == 0 || e.RemovedItems.Count > 0) return;
    var item = (CustomerViewModel) e.AddedItems[0];
    var container = (UIElement) listView1.ItemContainerGenerator.ContainerFromItem(item);
    container.Focus();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文