为列表框项目(WPF)设置焦点 [英] Set focus for Listbox item (WPF)

查看:65
本文介绍了为列表框项目(WPF)设置焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将最后添加的项目的焦点设置在列表框中,但是我无法获得有关如何使用View-model实现的确切逻辑

I am trying to set focus of the last added item in a listbox, however I am unable to get the exact logic on how to implement using View-model

以下是我的要求:

一个列表框,其中包含标签/文本框类型(1)的项目,这些项目是动态添加的

A list box which contains items of type label/textbox(either 1), these items are added dynamically

项目被添加,新项目应该是焦点

when an item is added, the new item should be in focus

此外,我应该有每个项目示例的上下文菜单:添加和删除,以便当用户选择任何项目和右键单击时应该出现上下文菜单。

Also, i there should be context menu for each item example: ADD and delete, such that when the user selects any item and right clicks the context menu should appear.

请问有人可以帮助解决上面的示例代码吗?

Please could someone help with an example code for the above?

推荐答案

嗨    Grace SC,



>>请有人帮忙查看示例代码以上?



你需要尝试一下。



>>我正在尝试设置列表框中最后添加的项目的焦点,但是我无法确切地获得关于如何使用View-model实现的逻辑



您可以尝试在WPF中设置与MVVM的ListBox SelectedItems绑定然后,尝试使用以下代码将最后添加的项目集中在列表框中。

Hi    Grace SC,

>>Please could someone help with an example code for the above?

You need to try on your side.

>>I am trying to set focus of the last added item in a listbox, however I am unable to get the exact logic on how to implement using View-model

You can try to set the ListBox SelectedItems binding with MVVM in WPF application.

Then, try use the following code to focus the last added item in a listbox.

        var item = lbTodoList.ItemContainerGenerator.ContainerFromItem(lbTodoList.SelectedItem) as ListBoxItem;
            item.Focus();

>>另外,我应该有每个上下文菜单item示例:添加和删除,这样当用户选择任何项目并右键单击时,应显示上下文菜单。

您可以添加ListBox.ContextMenu。

You can add ListBox.ContextMenu.

      <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Delete" Click="MenuItemDelete_Click"/>
                </ContextMenu>
            </ListBox.ContextMenu>




最好的问候



Yong Lu




Best regards

Yong Lu





这篇关于为列表框项目(WPF)设置焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆