Styling hard-to-reach elements in control templates with attached behaviours
来源:互联网 发布:python中注释快捷键 编辑:程序博客网 时间:2024/06/06 00:04
Styling hard-to-reach elements in control templates with attached behaviours
OK, the title of this blog post is not very snappy, but it is not an easy problem to describe in a few short words. Here’s the rub, the WPF DataGrid has a select-all button located in the top-left corner, just like Excel and many other grid controls / applications. However, with the default style, this button is barely visible and I would not be surprised if a user of the grid failed to see it.
Unfortunately restyling this button is not all that straightforward. The more complex WPF controls like the DataGrid and ListView typically expose the template used to construct, and the style applied to their various visual elements. However, the DataGrid does not expose a style or template for the select-all button.
Fortunately, all is not lost. With WPF, if a control does not expose a style for one of its component parts, you can replicate and replace its entire template. Sometimes this approach feels a little heavy-handed … “I can’t seem to find a way to fit those flashy alloy wheels to my car, so I’ll just by a new car that already has the wheels I like”.
I want to explore a more lightweight approach.
Whilst it is usually preferable to modify a controls template from XAML, in cases like this, I prefer the more concise approach of modifying them in code-behind. In order to do this, you need to handle the FrameworkElement.Loaded event on the control whos template you wish to modify. This event is fired after the control’s visual tree has been constructed, to verify this add a breakpoint in the event handler and inspect the visual tree with Mole. The image below shows the visual tree of my DataGrid which I have expanded to locate the select-all button.
You can see that the button is the first element, four steps down the visual tree, therefore it should be easy to locate by walking the visual tree. Once it has been reached we can simply replace its template to the one which we desire:
The template SelectAllButtonTemplate is simply defined as a resource of our Window.
This works just fine for our single grid, but what if we want to use the same trick on multiple DataGrids? (Besides, we have code-behind, which in WPF is a bit of a dirty word!).
The Attached Behaviour pattern is a useful WPF pattern that proves very useful in this situation. In brief, attached properties add state to a your WPF elements, whereas attached behaviours add behaviour. When an attached property becomes associated with a dependency object, an event is raised. We can handle this event and register handlers on the events of the object being attached to, in this case, our DataGrid’s Loaded event.
The following code is our attached behaviour in its entirety:
This attached property can be used as illustrated below, where we apply the SelectAllButtonTemplate from our Window’s resources to a DataGrid:
The result is a beautiful pink select-all button that I don’t think any user would miss in a hurry:
And a clean code-behind file for our Window
You can download the demo project for this blog post in the following zip file, wpfstylinghiddenelements.
One last thing … if you are implementing a WPF / Silverlight control, please expose the styles and templates for all component parts of your control!
Regards, Colin E.
- Styling hard-to-reach elements in control templates with attached behaviours
- Seeing numbers in Ruby with r attached to the number
- Vietnam: Footwear Exports to Reach $300mn in July
- The minimum number of elements to reach the end of an array
- Binding and styling text to a RichTextBox in WPF
- Using Templates With the ASP.NET Repeater Control...
- Emacs: How to Define Templates in YASnippet
- Visual Guide to Templates in Eclipse
- Failed to place enough replicas, still in need of 1 to reach 3. For more information。。。。
- Go Google: 20 Ways to Reach More Customers and Build Revenue with Google Business Tools
- HTML5 Placeholder Styling with CSS
- Use windows message to change text in CEdit with timeout control
- How To Use Git Source Control with Xcode in iOS 7
- Insert elements in vector to deque
- using inject to count elements in array
- Writing to Reach You [Lyric]
- Privileges play hard-to-get in Vista
- REACH
- WebLogic Server did not start up properly.的解决办法
- Python--关于for和if的一些
- 软件设计师(中级)复习方案
- 执行 drop table table_name 时,提示”资源正忙,需指定nowait
- HZK16 的应用小程序
- Styling hard-to-reach elements in control templates with attached behaviours
- .NET 水晶报表的使用实例 - [c#.net]
- 基于.NET的WebService的实现
- sql loader用法介绍
- 新十年,千万级ERP加速起航
- ASP.NET 安全认证(登录的使用)
- setsockopt 设置socket 详细用法
- win7配置android环境
- C#鼠标悬停提示