- 浏览: 401160 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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)
最新评论
Suppose that you have a UserControl and you want to merge some resources (which defines the styles, template and etc).
Suppose again that we cannot add the style to Application or Window, whereas you might conflict with styles that may be used by other part of the application (typical e.g. when the shell is designed by some developers but individual view is taken care by some other developers).
You may do something like this :
<UserControl x:Class="MyView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MyModule;component/Resource/Style/DarkStyles.xaml" /> <ResourceDictionary Source="pack://application:,,,/MyModule;component/Resource/Style/Styles.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> </UserControl>
This sound OK, however, suppose there are many of them, and you might need to do the copy and paste more than once..
which sound OK at initial sight, but look at the possibility of a change, see rename to the styles, things get ugly with copy&paste.
Suppose that the UserControl may use one/a group of fixed styles, then it make sense to have some way that you can easily pick up the styles.
One things comes to us is the attached property. below is the impl which leverage the attachedProperty, which accepts a string (in Pack Uri format) to the target style.
here are the code.
namespace MyNamespace { public class ResourceLoader { public static string GetResourcePackUri(DependencyObject obj) { return (string)obj.GetValue(ResourcePackUriProperty); } public static void SetResourcePackUri(DependencyObject obj, string value) { obj.SetValue(ResourcePackUriProperty, value); } // Using a DependencyProperty as the backing store for ResourcePackUri. This enables animation, styling, binding, etc... public static readonly DependencyProperty ResourcePackUriProperty = DependencyProperty.RegisterAttached("ResourcePackUri", typeof(string), typeof(ResourceLoader), new UIPropertyMetadata(string.Empty, ResourcePackUriChanged)); private static void ResourcePackUriChanged(object sender_, DependencyPropertyChangedEventArgs args) { //DependencyPropertyDescriptor dpDescriptor = DependencyPropertyDescriptor.FromName("ResourcePackUri", typeof(ResourceLoader), if (sender_ == null) throw new ArgumentNullException("sender_"); //DependencyPropertyDescriptor dpDescriptor = DependencyPropertyDescriptor.FromName("ResourcePackUri", typeof(ResourceLoader), sender_.GetType()); //if (dpDescriptor != null) //{ //} var dpObject = sender_ as DependencyObject; if (dpObject != null) { if (dpObject is UserControl) { var usercontrol = (UserControl)dpObject; var uriInString = (string)args.NewValue; var uriOld = (string)args.OldValue; if (!string.IsNullOrEmpty(uriInString)) { Uri uri = new Uri(uriInString); ResourceDictionary resourceNew = new ResourceDictionary(); resourceNew.Source = uri; if (!string.IsNullOrEmpty(uriOld)) { var resourceOld = usercontrol.Resources.MergedDictionaries.FirstOrDefault<ResourceDictionary>(r => r.Source == new Uri(uriOld)); if (resourceOld != null) usercontrol.Resources.MergedDictionaries.Remove(resourceOld); } usercontrol.Resources.MergedDictionaries.Add(resourceNew); } else { throw new ArgumentException("ResourcePackUri only accepts string values"); } } else { // ?? throw new ArgumentException("AttachedProperty ResourcePackUri is only applicable on UserControl"); } } } } }
after this, you may do this to add the attached property to the UserControl.
<UserControl x:Class="MyNamespace.MyView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyNamespace" local:ResourceLoader.ResourcePackUri="pack://application:,,,/MyModule;component/Resource/Style/MergedStyles.xaml" > </UserControl>
and if you look at the pack://application:,,,/MyModule;component/Resource/Style/MergeStyles.xaml, you will see something like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MyModule;component/Resource/Style/DarkStyles.xaml"/> <ResourceDictionary Source="pack://application:,,,/MyModule;component/Resource/Style/Styles.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
Follow this discussion, we mihgt looks into how to make use of the Custom Markup Extension to accomplish the same task.
发表评论
-
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 1422Topic: 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 ...
相关推荐
问题解决:Fragment not attached to Activity的相关代码,更多详细内容请参考:http://blog.csdn.net/u012939909/article/details/53355836
2018年最新的省市区联动数据表。一张表搞定全国所有省市区县。
Using an attached property 25 Creating an attached property 28 Accessing a static property from XAML 33 Creating a custom markup extension 37 Handling routed events 44 Chapter 2: Resources 51 ...
QHierarchy is an editor extension that adds several often used functions to hierarchy window: - Displaying the icon of a GameObject - Showing / hiding a GameObject - Locking / unlocking a GameObject...
Devphone toolkit开发控件 v2.0源码 项目描述: ... Zoom attached property Html attached property Clip attached property 提示: 如果有的项目不能正常加载,请尝试下载安装此程序(类库): ...
USB Attached SCSI(UAS),全称为Universal Serial Bus Attached SCSI,是一种在USB接口上实现SCSI协议的技术,旨在提高外部存储设备的数据传输速度和效率。UAS标准是为了解决传统USB Mass Storage Class(UMSC)...
json数据:{"result":1,"message":"获取信息成功","data":[{"ID":2,"NAME":"呵呵呵呵","TYPE_ID":22,"TEXT":"\u003cimg alt=\"\" src=\"/File/attached/image/20150421/20150421101617_6620.jpg","CREATEDATE":...
+ [enterprise] added property "Scheduler" - "StudioPath" in server configuration - set the path to FastReport Studio, leave blank for default - [enterprise] fixed bug with MIME types in http header ...
This interface is optimized for Enterprise and Client solid state drives, typically attached as a register level interface to the PCI Express interface. Note: During development, this specification ...
Attached to this post is my app that should interface with these logging services. Lets start by saying I HATE JAVA. Im an IT guy so i know my way around a few languages but im not the best so deal ...
There's two ways to use the MessageBar. It can either be attached directly to an activity, or a View can be passed. Attaching to an activity This approach requires adding the following attributes to ...
When mapping from an ER model to a relational model, a strong entity is mapped into a (a) table (b) row (c) column (d) key Correct answer is (a) 10. Which of the following is true about ...
Because some people need to use IP addresses instead of Host names, I‘ve added a new property IPAddress to SakPOP and SakSMTP. If both are filled, then the Host name will be used, thanks to Roger F. ...
SCGCQ00327929 Defect Command to enable Patrol Read on SSDs controller property is not shown in Help SCGCQ00331576 Defect LSI MegaCLI EncInfo (Enclosure Info) duplicates the first power supply status ...
*--- Desc. : Generate list of packages promoted to DIT2 and not */ /*--- : promoted to SIT2 for V2014 releases ... Get package attached to baselined releases */ /*--- : 3. Get package promotion history
This standard specifies the requirements for the USB Attached SCSI - 3 (UAS-3) transport ... This standard is intended to be used in conjunction with SCSI command set standards and USB specifications.
SAS (Serial Attached SCSI) 技术是一种先进的磁盘连接技术,它结合了并行SCSI(Small Computer System Interface)的稳定性和串行连接技术(如FC、SSA、IEEE1394)的高效性。SAS采用了串行通信协议,基于SCSI-3指令...
很抱歉,但根据您给出的信息,"ATTACHED" 和 "831" 并没有提供足够的上下文来生成一个超过1000字的详细IT知识文章。标题和描述都是相同的,而“字体”作为标签可能指的是文件内容涉及到字体设计、使用或技术。然而,...
When a CCyUSBDevice object is open to an attached USB device, its endpoint members provide an interface for performing data transfers to and from the device's endpoints. Endpoint-specific data members...