modify the visual tree at runtime
2013-04-26 00:00
274 查看
you may find sometime that you nee to alter the visual tree or the logical tree at runtime ot change some behavior, well this is not straight forward as it looks .
But a general rule that you have is that the Logical tree structure construct before the visual tree structure, which means that you can inject some methods to at the earlier stage of Visual Streee construction, where you have the logical tree in place, and then with the logical tree strucure in hand, you can start to make some changes to the visual tree.
here is some code where it shows given a structure like this:
But a general rule that you have is that the Logical tree structure construct before the visual tree structure, which means that you can inject some methods to at the earlier stage of Visual Streee construction, where you have the logical tree in place, and then with the logical tree strucure in hand, you can start to make some changes to the visual tree.
here is some code where it shows given a structure like this:
<UserControl> <UserControl.Resources> <Resources.MergedDictionaryResource> <Resource Source=".../> </Resources.MergedDictionaryResource> <UserControl> </UserControl> </UserControl>Where you want to remove some resource from the first UserControl, here is some code that shows you how to do that.
public partial class SearchResultsView : UserControl { public SearchResultsView() { InitializeComponent(); } protected override void OnVisualParentChanged(DependencyObject oldParent) { base.OnVisualParentChanged(oldParent); if (oldParent == null) // creation when oldParent == null { RemoveOnyxDarkTheme(); Trace.WriteLine("UIElement.UpdateLayout ... "); UpdateLayout(); } else { Trace.WriteLine("oops!!"); } //InvalidateArrange(); //base.OnVisualParentChanged(oldParent); //InvalidateVisual(); //this.UpdateLayout(); } private void RemoveOnyxDarkTheme() { DependencyObject parent = this.Parent; parent = GetLogicalParent<UserControl>(parent); if (parent != null) { var userControl = parent as UserControl; var list = new List<ResourceDictionary>(); if (userControl != null) { foreach (var item in userControl.Resources.MergedDictionaries) { try { if (!string.IsNullOrEmpty(item.Source.OriginalString) && item.Source.OriginalString.Contains("Nomura.Desktop.Themes.OnyxDark.DevExpress")) { list.Add(item); } else { if (string.IsNullOrEmpty(item.Source.OriginalString)) { Trace.WriteLine("Wrong pack URI??"); } } } catch (Exception ex) { Trace.WriteLine("Caught Exception, silently ignore : " + ex.ToString()); } } foreach (var item in list) { userControl.Resources.MergedDictionaries.Remove(item); } } } } private static DependencyObject GetParent(DependencyObject dObj) { if (dObj != null) return VisualTreeHelper.GetParent(dObj); return null; } private static DependencyObject GetParent(DependencyObject dObj, Type type) { if (dObj != null&& type != null) { while (GetParent(dObj) != null && GetParent(dObj).GetType() != type) dObj = GetParent(dObj); return dObj; } return null; } private static DependencyObject GetParent<T>(DependencyObject dObj) { return GetParent(dObj, typeof (T)); } private static DependencyObject GetLogicalParent(DependencyObject dObj) { if (dObj != null) return LogicalTreeHelper.GetParent(dObj); return null; } private static DependencyObject GetLogicalParent(DependencyObject dObj, Type type) { if (dObj != null && type != null) { while (GetLogicalParent(dObj) != null && GetLogicalParent(dObj).GetType() != type) dObj = GetLogicalParent(dObj); return dObj; } return null; } private static DependencyObject GetLogicalParent<T>(DependencyObject dObj) { return GetLogicalParent(dObj, typeof(T)); } }AS you can see the key is the OnVisualChanged method, which fires when the Visual Parent change (when it is created on the visual tree) , then you can grab the parent node and start cracking on .
相关文章推荐
- System.Runtime.Remoting.RemotingException: has been disconnected or does not exist at the server
- warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared librar
- Choosing the Type at Runtime
- Changing the SiteMapDataSource’s XML filename at runtime
- [XAF] How to define a business class at runtime or allow end-users to configure its members via the application UI?
- 运行时决定AAC硬件解码器可用性/Determining the availability of the AAC hardware encoder at runtime
- linux gcc编译错误:statically linked applications requires at runtime the shared libraries
- Error 1402 when installing Crystal Reports for Visual Studio 2010 runtime via the msi file
- C++ AMP: The Concurrency Runtime and Visual C++ 2010: Lambda Expressions
- Using WMI to Modify Settings at runtime
- Using 'dlopen' in statically linked applications requires at runtime the shared libraries
- Mailbag: How to detect the presence of the Visual C++ 9.0 runtime redistributable package
- How to Modify the Password for SYSMAN and other Enterprise Manager Users at the OMS Level and Reposi
- warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries f
- This view is not constrained vertically: at runtime it will jump to the left unless you add a vertic
- AS- This view is not constrained vertically. At runtime it will jump to the left/(0,0) unless you
- C++ AMP: The Concurrency Runtime and Visual C++ 2010: Lambda Expressions
- How to disable VirtualBox time sync from within the guest at runtime
- Code snippet of the day : Resizing ASP.NET Datagrid Controls in Edit mode at runtime
- Enumerate the visual tree and logic tree