积极答复者
如何在 ControlTemplate 中,设置 TemplateBinding Height -10 这样的操作?

问题
-
我有一个Label控件,我现在要对它设置ControlTemplate以重新呈现它的外观,代码如下
<ControlTemplate TargetType="Label" x:Key="PopFrame">
<Border Width="{TemplateBinding Label.Width}" Height="{TemplateBinding Label.Height}" CornerRadius="5" Background="{DynamicResource PopFrameRectangle1}" BitmapEffect="{DynamicResource PopEffect1}" Padding="5">
<Grid>
<Border CornerRadius="5" Padding="0,10,0,0" Background="{DynamicResource PopBrush1}">
<Rectangle RadiusX="5" RadiusY="5" Fill="{DynamicResource PopBrush2}" />
</Border>
<Path Name="mPath" Stretch="Fill" Stroke="#FF43B7FF" Fill="#FFFFFFFF" />
<Border Padding="15,45,15,15">
<Rectangle Fill="White" RadiusX="5" RadiusY="5" Stroke="#FFA19FA0" StrokeThickness="1" />
</Border>
</Grid>
</Border>
</ControlTemplate>
现在的问题是,中间的mPath的Data属性,是一堆的曲线,这些曲线的各个点,是要根据Label.Width和Label.Height来决定的,这里经常要用到Label.Width - 5 这样的操作,这种操作在CS代码里做是毫无问题的,但是在XAML里,该要如何实现呢?
附Data属性点设置代码
StreamGeometry geometry = new StreamGeometry();
geometry.FillRule = FillRule.Nonzero; //声前F0还是F1,现在是F1
using (StreamGeometryContext ctx = geometry.Open())
{
ctx.BeginFigure(new Point(5, 0), true, true);
ctx.LineTo(new Point(titlewidth, 0), true, false);
ctx.BezierTo(new Point(titlewidth + 28, 0), new Point(titlewidth + 4, 27), new Point(titlewidth + 60, 27), true, false);
ctx.LineTo(new Point(width, 27), true, false);
ctx.BezierTo(new Point(width + 2.5, 27), new Point(width + 5, 29.5), new Point(width + 5, 32), true, false);
ctx.LineTo(new Point(width + 5, height), true, false);
ctx.BezierTo(new Point(width + 5, height + 2.5), new Point(width + 2.5, height + 5), new Point(width, height + 5), true, false);
ctx.LineTo(new Point(5, height + 5), true, false);
ctx.BezierTo(new Point(2.5, height + 5), new Point(0, height + 2.5), new Point(0, height), true, false);
ctx.LineTo(new Point(0, 5), true, false);
ctx.BezierTo(new Point(0, 2.5), new Point(2.5, 0), new Point(5, 0), true, false);
}
geometry.Freeze();- 已编辑 n10z 2009年7月25日 3:38 增加问题说明
答案
-
你好,
-->这里经常要用到Label.Width - 5 这样的操作,这种操作在CS代码里做是毫无问题的,但是在XAML里,该要如何实现呢?
据我所知,这种逻辑运算(包括减法)在目前版本WPF的XAML中不支持的,你可以用Value converter在后台中实现,然后返回一个合适的值给binding.
如果还有疑问,你可以用email或是MSN联系我,欢迎交流
Email:v-jimz@microsoft.com
MSN: zhoujiguo1985@live.cn
谢谢。
Jim Zhou -MSFT- 已标记为答案 Jim Zhou - MSFTModerator 2009年7月29日 7:43
-
你可以参看我的这篇文章了解如何使用DataBinding和Value Converter来根据数据创建一些线段(当然你的情况应该不需要用到象我这样比较复杂的解决方案):
http://www.cnblogs.com/killmyday/archive/2009/07/27/1532230.html- 已标记为答案 Jim Zhou - MSFTModerator 2009年7月29日 7:43
全部回复
-
你好,
-->这里经常要用到Label.Width - 5 这样的操作,这种操作在CS代码里做是毫无问题的,但是在XAML里,该要如何实现呢?
据我所知,这种逻辑运算(包括减法)在目前版本WPF的XAML中不支持的,你可以用Value converter在后台中实现,然后返回一个合适的值给binding.
如果还有疑问,你可以用email或是MSN联系我,欢迎交流
Email:v-jimz@microsoft.com
MSN: zhoujiguo1985@live.cn
谢谢。
Jim Zhou -MSFT- 已标记为答案 Jim Zhou - MSFTModerator 2009年7月29日 7:43
-
你可以参看我的这篇文章了解如何使用DataBinding和Value Converter来根据数据创建一些线段(当然你的情况应该不需要用到象我这样比较复杂的解决方案):
http://www.cnblogs.com/killmyday/archive/2009/07/27/1532230.html- 已标记为答案 Jim Zhou - MSFTModerator 2009年7月29日 7:43