One context menu will apply to Group items and another context menu will apply to Leaf items. Now I want to attach a Context Menu to the items of the list. Your solution will allow me to double click the item label _or anywhere on the 'line' beside it_ to open it. However, if I double click to the right of “Group 2”, the default TreeView will not open it. Whereas your solution will highlight both the item label _and the entire 'line' beside it_Ģ) If I double click on the “Group 2” text, the default TreeView will open it. Given the following hiearchical structure (Hopefully my ASCII art is clear enough).ġ) If I select “Group 1”, the default TreeView will only highlight the “Group 1” label. Lee, Thank you for taking an interest in my question. It is frustrating that such a logical feature is not already built in by Microsoft and takes such effort to fix on our own. ItemTemplateSelector=”” HorizontalScrollBarVisibility=”Disabled” IsTabStop=”False” TabNavigation=”Once” VerticalScrollBarVisibility=”Auto”] Of course I can send more details if necessary. I'm sending only the parts that I think are relevants because the code is a bit lengthy. I hope this helps anyone else out there that has found this stumbling block to nice layout on TreeView controls Obviously you could align to the Left (default) or the other value such a Center, Right and Stretch. This now gives you a layout grid that looks more like this: Cell (0,0)Īllowing you to choose to fill the space available by setting HorizontalContentAlignment on the TreeViewItem (probably via a style). So here is a starter template that you can use to replace the default template which I think is probably what most users would expect from the default template any way. While this may seem like a big hammer for a little problem, my guess is that you were probably modifying the standard layout of a TreeViewItem substantially if you want it to stretch horizontally. We cant just tweak the template property of the TreeViewItem, we must completely replace it. It is nice to know that the solution is not to bad. By implementing the simple solution below the user gets same effect as the default control template, however they also get the flexibility of defining their own horizontal alignment Solution It also doesn’t help when M$ representatives constantly provided misleading information. Who knows?! I think that M$ have done an amazing job with the WPF framework in general, but there are a few things about the TreeView that are a little bit odd. Why would the default template be like this? Cell (0,0)Ĭell(1,1) + Cell(2,1) Children items via ItemsPresenter This effectively says column 1 can never effectively stretch. Ok, so now that we know that we look back up to the Grid and notice that the column definitions are such that Column 1 has Width=”Auto” and Column 2 has Width=”*”. Also notice that the ItemsPresenter (the place holder for all the children of a TreeViewItem) is set to row 1 and column 1 & 2 (Grid.Column=”1″ Grid.ColumnSpan=”2″). Now notice that the Border that encapsulates the ContentPresenter (the place holder for where your values will live) is set to live in column 1 (remember 0 based index rules apply here). Things to note here is that the layout is controlled with a 3×2 Grid. Lets take a look at a part of the Control Template for a TreeViewItem to find the problem: I have removed a lot of content that is not relevant to what our problems is. A ControlTemplate is a layout that is composed of other primitive controls such as Button, Selector, Grid etc. Its visual representation is composed in XAML by constructing a ControlTemplate and assigning it to the Control’s Template property. For some quick background, a control is just a DependencyObject made up of C# (or any other. If you explore the problem a little bit deeper you will find that the actual problem here is the way the Template for TreeViewItem controls has been defined in the two most popular themes (Luna for XP and Aero for Vista). However these “hacks” don’t address the underlying problem (hence why I am labelling them hacks). There are various hacks to get around it that have been suggested in the community: You would think that like any other scenario in WPF you would set either the HorizontalAlignment on the TreeViewItem or the HorizontalContentAlignment on a parent entity like the TreeView itself. It is the fairly simple requirements of having the header of an item in a Tree view fill all of the horizontal space available. An old problem I have faced popped up at one of new roles in the UK recently.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |