WPF:在ControlTemplate中使用TemplateBinding

本文介绍如何利用WPF中的TemplateBinding特性来优化控件模板的自适应性和美观性。通过实例演示了如何将按钮的属性绑定到内部Ellipse控件上,实现高度和背景颜色等属性的自动同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A bit on TemplateBinding and how to use it inside a ControlTemplate.

Introductio

Today I'll try to write a bit on TemplateBinding and how to use it inside a ControlTemplate.TemplateBinding is a type of binding used mainly for template scenarios. Here I am not going to write more on its theoretical aspect as what is TemplateBinding, when to use, blah blah blah, as lot of content is readily available on the net.  So, let's start quickly with the coding part:

First of all, let's create a new project using WPF template and place a button in it as below:

1.png

Now, what I am going to do is, I am going to replace this content template for this button. So, in order to do this, open up the Button tag and add Button.Template markup tag with a new ControlTemplate as:

2.png

Now as soon as you will add ControlTemplate tag, you will notice that the content of the button is gone and button is shown as a transparent rectangle. This has happened because here I told WPF to replace the defaultControlTemplate with the one which I defined. But at this point, our ControlTemplate is blank, so there is no visualization and we can see only a transparent rectangle.

Now go ahead and customize our ControlTemplate by putting Ellipse inside it as:

3.png

Now we can see that we get a visualization for a button in the form of ellipse. At this point of time, it works OK, but there are scenarios where this struct breaks down.

For example, let's increase the height of button, from 35 to 105 as:

4.png

In the above image, you will notice that button height is increased but the ellipse size is still the same, which is a bad UI design. And the reason this is happening is, inside a ControlTemplate, the height of an ellipse is hard coded. So, no matter, whatever height is set at parent (i.e., Button), it will not get inherited to child control (i.e.Ellipse).

So, now we have to fix this problem by using a special type of binding called TemplateBinding insideControlTemplate. So, instead of hard coding the height, we will use TemplateBinding as shown below:

5.png

By setting the TargetType property of ControlTemplate, we are telling Ellipse that, any property of Button can be set to ellipse. Now, whatever the height of button will be, it will automatically be the height of ellipse also. Isn't it interesting?

Moving forward, let's do something more interesting with Fill property of ellipse.

6.png

In the above snippet, I am trying to set the Fill property of an ellipse using TemplateBinding. But now the problem here is, a button doesn't have a Fill property. So, there is no one-to-one mapping for Fillproperty. Now, what to do?

No need to worry that much because button does have a Background property as:

7.png

In the above image, you might have noticed that as soon as ellipse's Fill property is set to Background,ellipse becomes transparent as button's background. Now if we set button's Background property to Red, the same will be applied to ellipse too.

8.png

So, one can understand how much magic we can do with TemplateBinding.
Now, let's work a little bit on code cleanup.

ControlTemplate Inside Resource Dictionary

For better code readability, we will move out our ControlTemplate code and put it inside a resource dictionary as:

9.png

So, now we can see as earlier that whatever visual property for button is set, all get applied to ellipse as well.

Hope this tip was useful and gave you the basic idea on how to use TemplateBinding.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值