WPF Tutorial - Using A Visual Collection
While WPF and XAML make the common 90% of UI programming quite easy, sometimes it gets a little odd in those other 10%. For instance - the visual tree. Most of the time it works great, and you never need to do anything special with it. But what about when you specifically want to give a user control children? Or maybe you want to give an adorner an actual visual child, instead of using OnRender? It isn't really obvious right away how to go about doing that.
But while it is not obvious, it is possible - and so today we are going to take a look at using VisualCollection to do those things. Specifically, we are going to create an Adorner to be used in an AdornerLayer that accepts a Visual as content. This is not possible right out of the gate, because Adorners don't have any built in way to have Visual children, and we can't use any of the normal controls that do (Panel, ContentPresenter, etc...), because only Adorners can be placed on an AdornerLayer.
Now, the example we are going to build today to use this adorner that can present content is pretty silly, but it is actually quite useful. I've used it a number of times for drag and drop (giving visual representations of what is being dragged and what will happen when it drops). Ok, now for some code:
public class AdornerContentPresenter : Adorner
{
private VisualCollection _Visuals;
private ContentPresenter _ContentPresenter;
public AdornerContentPresenter(UIElement adornedElement)
: base(adornedElement)
{
_Visuals = new VisualCollection(this);
_ContentPresenter = new ContentPresenter();
_Visuals.Add(_ContentPresenter);
}
public AdornerContentPresenter(UIElement adornedElement, Visual content)
: this(adornedElement)
{ Content = content; }
protected override Size MeasureOverride(Size constraint)
{
_ContentPresenter.Measure(constraint);
return _ContentPresenter.DesiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
_ContentPresenter.Arrange(new Rect(0, 0,
finalSize.Width, finalSize.Height));
return _ContentPresenter.RenderSize;
}
protected override Visual GetVisualChild(int index)
{ return _Visuals[index]; }
protected override int VisualChildrenCount
{ get { return _Visuals.Count; } }
public object Content
{
get { return _ContentPresenter.Content; }
set { _ContentPresenter.Content = value; }
}
}
That is the entirety of the code for the AdornerContentPresenter. Not a whole lot of code, but a lot is going on, so I'll go though each function. First off, the VisualCollection. As you can see in the constructor, we create a VisualCollection right off the bat. The constructor for a VisualCollection takes a single argument - the owner. The owner is always the object that will be displaying the items in the collection, so in this case it is the Adorner itself. Next, we make a ContentPresenter and add it to the VisualCollection. You might think that is a little odd, but it actually makes our life a good bit easier.
You see, the content that is given to this will always be handed to this ContentPresenter. The ContentPresenter is good at exactly one thing - presenting pretty much any type of content. This way, we don't have to worry about how to display any type of content - we just have to worry about displaying a ContentPresenter, and the ContentPresenter worries about all the complex stuff. So this ContentPresenter will always be the only member of the VisualCollection for the Adorner.
Ok, now on to that second constructor - nothing special here, this is just a convenience to set the content right during creation.
Next, the measure and arrange. This is where using the ContentPresenter comes in handy. Pretty much, we just pass through the Measure and Arrange calls straight to the ContentPresenter, and return the values that comes back (the DesiredSize property for Measure, and the RenderSize property for arrange).
Following those two methods are two items that you have probably never had a reason to mess with before: GetVisualChild and VisualChildrenCount. These are extremely important to override whenever you start playing with the children in a control. Since we have an internal VisualCollection, we can just pass the requests on to that collection. For GetVisualChild, we return the item at the requested index from the VisualCollection, and for VisualChildrenCount we return the number of items in the VisualCollection.
Finally, the Content property. All that this property does is set or get the content of our internal ContentPresenter. And thats it!
Now, you are probably wondering why I have the VisualCollection in this class at all - I sure did at first. On the surface it seems that it serves no purpose at all - and in fact stuff would "mostly" work if you weren't using it. Actually, for a while, in that Drag+Drop Adorner I was talking about earlier, I didn't use a VisualCollection, and nothing seemed wrong - until certain stuff just didn't work (like hit testing and global styles). It turns out that the VisualCollection does do some very important work underneath the surface - it maintains the parent-child connections between the owner of the collection and any items added to it. And, after some digging, it turns out that using a VisualCollection is one of two possible ways to maintain these connections. The other way is to use the protected methods AddVisualChild and RemoveVisualChild, although Microsoft generally recommends using a VisualCollection over using those two methods.
Ok, now for the silly example for how to use this AdornerContentPresenter:
<Window x:Class="VisualCollectionExample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="300">
<AdornerDecorator>
<Rectangle Fill="Black" Width="50" Height="50" x:Name="Block" />
</AdornerDecorator>
</Window>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
AdornerLayer layer =
AdornerLayer.GetAdornerLayer(Block);
AdornerContentPresenter adc =
new AdornerContentPresenter(Block);
adc.Content = new TextBlock() {
Text = "Hi There",
Foreground = Brushes.White
};
layer.Add(adc);
}
}
Exactly what you probably expected. That code will show a black rectangle, and on top of the rectangle will the the text "Hi There" in white in the adorner layer.
Well, that is it for using a VisualCollection. Just is case you were wondering, there is an equivalent collection called UIElementCollection that is used when you want to automatically maintain the connections for both the WPF visual and logical trees - but perhaps that is content for a future tutorial. For now, you can grab the Visual Studio solution for the code above here.
Source Files:
WPF Tutorial - Using A Visual Collection的更多相关文章
- WPF设置VistualBrush的Visual属性制作图片放大镜效果
原文:WPF设置VistualBrush的Visual属性制作图片放大镜效果 效果图片:原理:设置VistualBrush的Visual属性,利用它的Viewbox属性进行缩放. XAML代码:// ...
- WPF 获取元素(Visual)相对于屏幕设备的缩放比例,可用于清晰显示图片
原文:WPF 获取元素(Visual)相对于屏幕设备的缩放比例,可用于清晰显示图片 我们知道,在 WPF 中的坐标单位不是屏幕像素单位,所以如果需要知道某个控件的像素尺寸,以便做一些与屏幕像素尺寸相关 ...
- wpf异常:指定的 Visual 不是此 Visual 的上级问题处理解析
WPF在画线的时候,调用Control0.TransformToAncestor(Control1).Transform(new System.Windows.Point(0, 0))方法转换坐标的时 ...
- WPF 只读集合在 XAML 中的绑定(WPF:Binding for readonly collection in xaml)
问题背景 某一天,我想做一个签到打卡的日历.基于 Calendar,想实现这个目标,于是找到了它的 SelectedDates 属性,用于标记签到过的日期. 问题来了. 基于MVVM模式,想将其在xa ...
- Tutorial: WPF User Control for AX2012
原作者: https://community.dynamics.com/ax/b/goshoom/archive/2011/10/06/tutorial-wpf-user-control-for-ax ...
- WPF/MVVM Quick Start Tutorial - WPF/MVVM 快速入门教程 -原文,翻译及一点自己的补充
转载自 https://www.codeproject.com/articles/165368/wpf-mvvm-quick-start-tutorial WPF/MVVM Quick Start T ...
- WPF - Visual调试工具Snoop
原文:WPF - Visual调试工具Snoop Snoop经过很长一段时间,最近更新到支持NET 3.5了,它是一个WPF运行时对Visual UI调试的一个工具,最近我用过它调试修改过一个bug, ...
- [No000012E]WPF(6/7):概念绑定
WPF 的体系结构,标记扩展,依赖属性,逻辑树/可视化树,布局,转换等.今天,我们将讨论 WPF 最重要的一部分——绑定.WPF 带来了优秀的数据绑定方式,可以让我们绑定数据对象,这样每次对象发生更改 ...
- [No000012D]WPF(5/7)依赖属性
介绍 WPF带来了很多传统 Windows 应用程序没有的新特性和选择.我们已经讨论了一些 WPF 的特性,是时候更进一步介绍其他特性了.当你读完这个系列之前的文章,我希望你已经或多或少地了解了 WP ...
随机推荐
- PHP数组排序sort、asort与ksort用法
分享下PHP数组排序之sort.asort与ksort用法,实例中简单示范了sort.asort与ksort的用法,并备有注释加以详细说明. PHP数组排序中sort.asort与ksort的用法. ...
- jquery 取子节点及当前节点属性值
分享下jquery取子节点及当前节点属性值的方法. <li class="menulink"><a href="#" rel="ex ...
- Android性能优化系列之apk瘦身
Android性能优化系列之布局优化 Android性能优化系列之内存优化 为什么APK要瘦身.APK越大,在下载安装过程中.他们耗费的流量会越多,安装等待时间也会越长:对于产品本身,意味着下载转化率 ...
- rpmverify命令用来验证已安装的rpm软件包的正确性
-Va:验证所有软件包: 来自: http://man.linuxde.net/rpmverify -Va:验证所有软件包: [root@DB ~]# rpmverify -Va ....L.... ...
- lua 工具类(一)
-- -- Author: My Name -- Date: 2013-12-16 18:52:11 -- csv解析 -- -- 去掉字符串左空白 local function trim_left( ...
- Angular的重和利
1.第一重:TypeScript,TypeScript语言的特性还是比较丰富的,而且一直在发展,再就是跨语言集成问题,要想Nice对第三方lib做集成,需要自己写d.ts,针对有些第三方库,这件事情有 ...
- 一个更好的C++序列化/反序列化库Kapok
KapokFAQ1.Kapok的特点简单,易用,header-only,只需要引用Kapok.hpp即可:高效,初步测试性和messagepack相当.它是纯c++11实现,因此需要支持C++11的编 ...
- 如何使用IDEA开发工具中右键中的Git图形化工具
首先,你的项目一定是git服务器上面down下来的,下面来演示如何使用IntelliJ IDEA 开发中在鼠标右键中提供的一个非常方便的图形化Git管理工具: 这里使用的IDEA开发工具的版本是 In ...
- mysql load本地文件失败,提示access denied
mysql load本地文件失败,提示access denied 解决方式 直接谷歌到stackoverflow,解决方式如下 mysql -u myuser -p --local-infile so ...
- FIDDLER的使用方法及技巧总结(连载四)FIDDLER通用规则更改
四.FIDDLER通用规则更改 To make custom changes to web requests and responses, use FiddlerScript to add rules ...