WPF about Treeview and contextmenu

Source: Internet
Author: User

I have encountered a bug over the past few days. This bug has not been perfectly solved yet, but I have a lot of knowledge about WPF. To prevent forgetting what I have learned, I keep the web pages with all the knowledge and information I have found open. You cannot always open dozens of webpages. Therefore, you can summarize valuable information and write it into this article, which will be organized later. Chinese characters cannot be found in Google, so they must be remembered in English. The required information can often be found in English.

1. Treeview indicates the tree structure of WPF. If you want to make the Treeview sub-item contain an attribute, you can use itemcontainerstyle to define the treeviewitem style in a unified manner and apply hierarchicaldatatemplate. For example, you can define a dockpanel container in it, and then put some textblocks in it, and you can right-click the dockpanel and define the contextmenu for the dockpanel.

2. According to the above definition, the contextmenu can be displayed when you right-click it, but it cannot be displayed when you press the menu key. Shift + F10 can also be used for Keyboard Events popped up by right-click. See http://social.msdn.microsoft.com/Forums/en/wpf/thread/01d0e981-156c-4dff-a588-bbe184cef398 for details? Prof = required

3. The menu key is the key on the left of CTRL on the right, and the corresponding key enumeration is key. apps. F10 is often the system key. Therefore, determine whether SHIFT + F10 uses the followingCode: Keyboard. modifiers = modifierkeys. Shift & (E. systemkey = key. F10 | E. Key = key. F10 ).

4. You can set itemcontainerstyle of contextmenu through setter and eventsenter to set the attributes of all items.

5. verticaloffset can be used to set the vertical offset. contextmenuservice. setverticaloffset (uielement) can be used to set the vertical offset of contextmenu.

6. contextmenuservice. placementmode (uielement) can be used to set the placement method of contextmenu. For example, if it is set to top, it can be placed on the element. If it is set to relative, offset is performed based on the upper left corner, the offset is the value set in 5.

7. Setting the offset of the contextmenu in XAML will make the contextmenu have the offset no matter what the placementmode is.

8. if you follow the 1 method, the control in the keyboard event and hierarchicaldatatemplate won't get the focus, because the focus will always be obtained by the corresponding treeviewitem. Therefore, press the menu key on the control, the right-click menu is not displayed because there is no focus.

9. The Treeview has selecteditem and the selected item is obtained. If the itemsource of the selected item is observablecollection <Object>, the corresponding object is returned.

10. treeviewitem has a selected event, and you can get the selected treeviewitem in the case of 9. You can set the tag of the Treeview in it to get the selected treeviewitem in the future. The Code obtained is as follows:

Private void treeview_onitemselected (Object sender, routedeventargs E)
{
Treeview. Tag = E. originalsource;

} Of course, this can also be obtained through the sender, but the sender needs to consider the Event Routing to determine what item triggers the event.

11. each control has the focus Method, call, obtain the focus, and obtain the focus area. If a vertex obtains the focus, the upper layer of the vertex has corresponding events, for example, a button on the panel, the button has a click event. If the button is not processed, the Panel will also handle the event. The details can be viewed in the Event Routing http://msdn.microsoft.com/zh-cn/magazine/cc785480.aspx.

12. You can use Treeview. itemcontainergenerator. containerfromitem (Treeview. selecteditem) to obtain the treeviewitem corresponding to the item of the lower-level node of the current node (as set in 9 ). See: http://social.msdn.microsoft.com/Forums/zh-CN/silverlightzhchs/thread/ec4856c0-548e-4262-a9cb-4829b596ab65/ for details

13. You can use the findname method to find items in hierarchicaldatatemplate or other datatemplate, the specific method is as follows: http://msdn.microsoft.com/zh-cn/library/system.windows.frameworktemplate.findname.aspx

14. After previewkeydown, dockpanel and various containers seem to change the focus to be verified.

15. The keyboard and mouse focus are different. For details, refer to keyboard. focusedelement.

16. It seems that the Event Routing trend in WPF is from top to bottom in the preview event; otherwise, it is from bottom to top. I did not take a closer look at this ......

17. You can focus on the control inside when you select a treeviewitem. For details, see: http://stackoverflow.com/questions/1612566/how-to-set-focus-to-a-control-in-a-treeviewitem-when-selected

18. tooltip is the item displayed when the mouse moves up.

19. You can set whether the focusable attribute of uielement can be focused. You can also use the focusmanager static class to handle focus events.

20. You can use E. Handled to set whether the event route will continue to respond or stop on a control.

It will be organized in the future to make the valuable display clearer and more systematic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.