visualtreehelper

Why does the Parent property of a container of an ItemsControl return null and not the Panel it sits on?

天涯浪子 提交于 2019-12-30 08:07:49
问题 This one has me stumped. We have a custom ItemsControl which uses both custom containers as well as a custom panel as its ItemsHost. Now the panel has some metrics that the containers need for rendering purposes. Since they are direct children of the panel in the visual tree, you'd think that the Parent property of the container would return the panel, but it doesn't! I have also confirmed this exact thing using Snoop on a standard ListBox so this isn't exclusive to our code, but apparently

Get TreeViewItem for TreeView logical element

老子叫甜甜 提交于 2019-12-25 02:28:09
问题 My tree view seems like this <TreeView x:Name="ArticlesTreeView" Grid.Column="0" AllowDrop="True"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type structure:NewsPaperDocument}" ItemsSource="{Binding Children}"> <TextBlock Text="{Binding Name}" Tag="{Binding Object}" FontWeight="Bold" /> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type structure:NewsPaperPage}" ItemsSource="{Binding Children}"> <TextBlock Text="{Binding Name}" Tag="{Binding Object}"

Getting the Logical UIElement under the mouse in WPF

一笑奈何 提交于 2019-12-23 13:15:51
问题 It seems that all of the way of retrieving an element under the Mouse relates to Visual Hit testing. Is there some mechanism that I'm missing which would allow me to grab the actual UIElement that represents the current visual tree that the HitTest returns? Summary of what I'm doing: I have a custom tooltip class which relies on doing something based on the UIElement that the mouse is over. Simply put, it hooks into the owning Window's PreviewMouseMove event and updates a "current Item". This

Get the parent listview from a gridview object

ぐ巨炮叔叔 提交于 2019-12-22 10:41:16
问题 In the code-behind of a WPF application I have a variable containing a GridView. I know for sure that this GridView is the View of a ListView. Is there any way to get a reference to that ListView? Thanks 回答1: http://www.hardcodet.net/2008/02/find-wpf-parent We've been using these helper classes for a while to find visual elements in the visual tree. In this case, you'd just want to use the method and it will hunt down the visual ancestor. TryFindParent<ListView>(yourGridView); 来源: https:/

FrameworkElement.Parent and VisualtreeHelper.GetParent behaves differently

巧了我就是萌 提交于 2019-12-20 02:29:54
问题 In a WP7 app, I used FrameworkElement.Parent recursively to determine whether a specific element is inside of another element. But it does not always work correctly. Then I changed my code to use VisualtreeHelper.GetParent method recursively, and it always work correctly(so far as I tested). So what is the difference of them? Thanks 回答1: Consider ControlA and ControlB where ControlA is a content control meaning it can contain other controls. <ControlA> <ControlB /> </ControlA> Logically the

ItemsControl has no children during MainWindow's constructor

我的未来我决定 提交于 2019-12-12 17:24:24
问题 Based on the answer to SO question "WPF: arranging collection items in a grid", I have the following: <ItemsControl Name="itemsControl1" ItemsSource="{Binding MyItems}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid Name="theGrid" ShowGridLines="True" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style TargetType="{x:Type FrameworkElement}"> <Setter Property="Grid.Row" Value="{Binding RowIndex}" /> <Setter Property="Grid.Column" Value="{Binding

WPF ViewBox blocks VisualTreeHelper search

混江龙づ霸主 提交于 2019-12-11 15:17:02
问题 I have tried this Navigation Service approach (View First) for MVVM-Light WPF https://stackoverflow.com/a/28968560/5272185 I realized that the VisualTreeHelper search for the Frame element does not find the Frame if the Frame is located inside a ViewBox. Is there a good explanation of such strange behaviour? Is it possible to work around it? 回答1: It turned out that the example code in the link was the problem. If you run into this type of problem I recommend to read the following from Josh

VisualTreeHelper not finding children of DependencyObject, How can I reliably find objects?

青春壹個敷衍的年華 提交于 2019-12-10 12:09:14
问题 I have a UserControl called ZoneContainer . This has a property that which contains a ListBox containing a number of ListItem s. Each ListItem contains a DockPanel . I'm trying to use a the following code to find the children that exist inside ZoneContainer but childrenCount is 0 every time. var parent = this as DependencyObject; // I can see that this is populated. int childrenCount = VisualTreeHelper.GetChildrenCount(parent); Is there another way to find a specific child object inside a

How to Loop Through CheckBoxes in a TabControl using WPF?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 11:53:30
问题 I am trying to loop through child elements of a Tab Control to know what Check Boxes are set to checked or not checked. I have found various answers on SO, but I can't seem to get the code to do what I need. Here is what I have thus far: foreach (System.Windows.Controls.TabItem page in this.MainWindowTabControl.Items) { if(VisualTreeHelper.GetChild(page, 0).GetType() == typeof(System.Windows.Controls.Grid)) { var grid = VisualTreeHelper.GetChild(page, 0); int gridChildCount = VisualTreeHelper

FrameworkElement.Parent and VisualtreeHelper.GetParent behaves differently

本秂侑毒 提交于 2019-12-01 21:07:21
In a WP7 app, I used FrameworkElement.Parent recursively to determine whether a specific element is inside of another element. But it does not always work correctly. Then I changed my code to use VisualtreeHelper.GetParent method recursively, and it always work correctly(so far as I tested). So what is the difference of them? Thanks Consider ControlA and ControlB where ControlA is a content control meaning it can contain other controls. <ControlA> <ControlB /> </ControlA> Logically the Parent property of the ControlB instance is an instance of ControlA in this case. However if you were to look