- 浏览: 401112 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
In this page: Understanding the Visual Tree and Logical Tree in WPF , the difference between the Visual Tree and Logical Tree is discussed.
Visual Tree and Logical Tree
In summary.
Visual Tree
- represents all of the elements in your UI which render to an output device (typically, the screen)
- Used for rendering, event routing, and locating resource (if an element has no logical parent)
Logical Tree
- Represents the essential structure of your UI. It closely matches the elemtns you declared in XAML, and excludes most visual elements create internally to help render the elements you declared.
- WPF use logical tree to determine several things including dependency proeprty value inheritence, resource resolution and more.
How to walk down/up the VisualTree and the Logical Tree
with the Help of VisualTreeHelper, you can do
- VisualTreeHelper.GetParent(depObj)
- VisualTreeHelper.GetChildren(depObj)
and with the help of LogicalTreeHelper, you can do
- LogicalTreeHelper.GetParent(depObj);
- LogicalTreeHelper.GetChildren(depObj);
Below shows some helper function to help you find a parent of a specific type
DependencyObject parent = ExVisualTreeHelper.FindVisualParent<UserControl>(this); public static class ExVisualTreeHelper { /// <summary> /// Finds the visual parent. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sender">The sender.</param> /// <returns></returns> public static T FindVisualParent<T>(DependencyObject sender) where T : DependencyObject { if (sender == null) { return (null); } else if (VisualTreeHelper.GetParent(sender) is T) { return (VisualTreeHelper.GetParent(sender) as T); } else { DependencyObject parent = VisualTreeHelper.GetParent(sender); return (FindVisualParent<T>(parent)); } }
You can do the same for LogicalTree.
Remove Visual/Logical Child
You may sometimes require to remove a child from a visual tree or logical tree (actually still investigating when this is justified).
There are two method for the class FrameworkElement.
FrameworkElement.RemoveLogicalChild
FrameworkElement.RemoveVisualChild
But they are protected internal method. Generally there are two ways to get around this.
- inherit and override
- reflection call.
Below show the code to do the reflection call.
private System.Reflection.MethodInfo GetMethodInfo(string name) { if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); var windowType = typeof(FrameworkElement); var methodinfo = windowType.GetMethod(name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod); if (methodinfo != null) return methodinfo; return null; } private void CallRemoveLogicalChild(FrameworkElement window, object child) { if (window == null) throw new ArgumentNullException("window"); if (child == null) throw new ArgumentNullException("child"); var method = GetMethodInfo("RemoveLogicalChild"); if (method != null) { method.Invoke(window, new[] { child }); } } private void CallRemoveVisualChild(FrameworkElement window, object child) { if (window == null) throw new ArgumentNullException("window"); if (child == null) throw new ArgumentNullException("child"); var method = GetMethodInfo("RemoveVisualChild"); if (method != null) { method.Invoke(window, new[] { child }); } }
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1979first let’s see an example ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1421Topic: WPF – Virtualization – ... -
wpf - BehaviorBase and one use examples
2013-06-18 18:41 1318Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1925You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5337As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1191Recently I was trying to debug ... -
wpf - default implicit style
2013-05-10 10:24 796We know that if you left out ... -
wpf - Style setter on the attached property
2013-05-08 14:54 2857I believe that you are familia ... -
wpf - specify enum values in xaml
2013-05-08 11:31 3594Many a situation you find tha ... -
wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island
2012-12-18 14:28 1290Sometimes you may bind to some ... -
wpf - translate winform button/mouse event to wpf events
2012-12-12 17:37 2166It is common that we sometimes ... -
wpf - Freezable and its meaning
2012-09-27 12:38 0This article is based on the di ... -
wpf - Customize the grid lines for original wpf Grid control
2012-09-27 12:01 1461The System.WIndows.Controls.Gri ... -
c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
2012-09-25 14:27 7421In Previous discussion, we have ... -
wpf - Get Effective DependencyProperty value on a DependencyObject
2012-08-28 19:05 1048As discussed in my previous pos ... -
wpf - Default(Theme) style and its DefaultStyleKey
2012-08-28 17:54 1392As dicsused in the subsection o ... -
wpf - Dependency Property Value Precedence
2012-08-28 18:56 886A dependency property to an Dep ... -
wpf - WPF TemplateBinding vs RelativeSource TemplatedParent
2012-08-28 14:20 3719This is a post that summarizes ... -
wpf - ICutomTypeDescriptor , PropertyDescriptor and its use in PropertyGrid
2012-08-28 14:04 3589The type ICustomTypeDe ... -
wpf - tips to convert UI controls in WPF/Silverlight/Winforms into a Bitmap
2012-08-27 17:44 986In previous discussion, we have ...
相关推荐
**Xceed.WPF.Toolkit库详解** Xceed.WPF.Toolkit是一个强大的WPF(Windows Presentation Foundation)扩展库,由Xceed Software公司开发。这个库为开发者提供了许多预构建的UI控件和功能,使得在构建现代、美观且...
**WPF Extended.Wpf.Toolkit加载界面Demo** WPF(Windows Presentation Foundation)是.NET框架的一部分,用于构建具有丰富图形、多媒体和数据绑定功能的桌面应用程序。在开发WPF应用时,我们常常需要为用户提供一...
《Xceed.Wpf.AvalonDock:一个强大的WPF文档管理库》 Xceed.Wpf.AvalonDock是一款专门用于Windows Presentation Foundation (WPF) 应用程序的库,它提供了一种高度可定制和灵活的文档和工具窗口管理解决方案。这个...
Syncfusion.Shared.Wpf.dll
从当当网购买的书籍内附原版、英文资料文件,原汁原味。
标题“InteractiveDataDisplay.WPF.rar”暗示了我们正在讨论的是InteractiveDataDisplay库的一个版本,它以RAR压缩格式提供,可能是包含不同.NET框架版本的支持文件,如net46和net452。这表明该库可能兼容.NET ...
在本文中,我们将深入探讨如何使用WPF(Windows Presentation Foundation)和Vlc.DotNet.Wpf库来封装一个自定义的视频播放器。WPF是.NET Framework中的一个UI框架,提供了丰富的图形渲染和用户交互能力。而Vlc....
gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...
Syncfusion.Chart.Wpf.dll
Wpf客户端框架,AIStudio.Wpf.AClient6.0,全新优化。AIStudio.Wpf.AClient是一个基于C#的开源项目,利用了WPF框架来构建用户界面。这个项目的主要目标是提供一个易于使用的界面,让用户能够轻松地与AIStudio进行...
WPF 24小时自学教程 Teach.Yourself.WPF.in.24.Hour
《Microsoft.Wpf.Interop.DirectX.dll:Windows Presentation Foundation与DirectX的桥梁》 在Windows应用程序开发领域,Microsoft.Wpf.Interop.DirectX.dll是一个至关重要的组件,它为Windows Presentation ...
DotNetBar.for.WPF.v5.9.0.0-AHCU
PVZHelper.Wpf.exe
Sams.Teach.Yourself.WPF.in.24.Hours.pdf
《Foundation.Expression.Blend.2.Building.Applications.in.WPF.and.Silverlight》一书由Victor Gaudioso撰写,深入探讨了如何利用Microsoft Expression Blend 2构建WPF(Windows Presentation Foundation)和...
WPF.Themes.ThemeManager.ApplyTheme(Application.Current, Properties.Settings.Default.Themes);//读取设置界面主题配置信息 即可使所有常用的控件主题变成所选择的主题,无需在每个窗体里修改样式。 减少了很...
扩展WPF工具包™是WPF控件,组件和工具,用于创建下一代Windows应用程序的数量一个集合。用它来构建专业的,现代的,易于使用的业务线应用程序。扩展WPF工具包项目已经在这里和下载的NuGet超过50万次。
WPF3.1引用库,无水印,简单易实现