generic.xaml 中的 TemplateBinding 和属性上的重绘已更改
我有一个自定义控件。在自定义控件中,有几个元素。这些元素之一应该具有特殊的高度值。
我所说的这个高度是以下代码中的 CanvasThickness:
private double canvasThickness;
public static readonly DependencyProperty CanvasThicknessProperty =
DependencyProperty.Register("CanvasThickness",
typeof(double),
typeof(CustomControl1),
new FrameworkPropertyMetadata(3d));
public double CanvasThickness
{
get { return canvasThickness; }
set { canvasThickness = value; }
}
在 generic.xaml 中,这个 CanvasThickness 用于 Canvas 的 Height-Property:
<ControlTemplate x:Key="SliderTemplate" TargetType="{x:Type Slider}">
<Canvas Width="25" Height="{TemplateBinding local:CustomControl1.CanvasThickness}" Background="Green">
// Templating Slider
</Canvas>
</ControlTemplate>
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Canvas Width="50" Height="20" Background="GreenYellow">
<Slider Template="{StaticResource SliderTemplate}" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在这个 Slider 的高度为 3。 假设我像这样使用 CustomControl:
<ctrl:CustomControl1 CanvasThickness="12"/>
I会假设 Slider 的高度为 12。但它仍然是 3。CanvasThickness 的值为 12。
如何重新绘制 CustomControl属性改变了?我尝试了 FrameworkPropertyMetadataOptions,但它不影响 CustomControl。
提前致谢。
编辑:如果可能,解决方案也应该在 Silverlight 中运行。
I've a custom control. In the custom controls, there are several elements. One of those element should have a special height value.
This height I'm talking about is CanvasThickness in the following code:
private double canvasThickness;
public static readonly DependencyProperty CanvasThicknessProperty =
DependencyProperty.Register("CanvasThickness",
typeof(double),
typeof(CustomControl1),
new FrameworkPropertyMetadata(3d));
public double CanvasThickness
{
get { return canvasThickness; }
set { canvasThickness = value; }
}
In generic.xaml is this CanvasThickness used for the Height-Property of a Canvas:
<ControlTemplate x:Key="SliderTemplate" TargetType="{x:Type Slider}">
<Canvas Width="25" Height="{TemplateBinding local:CustomControl1.CanvasThickness}" Background="Green">
// Templating Slider
</Canvas>
</ControlTemplate>
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Canvas Width="50" Height="20" Background="GreenYellow">
<Slider Template="{StaticResource SliderTemplate}" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now this Slider has a height of 3. Let's say I use the CustomControl like this:
<ctrl:CustomControl1 CanvasThickness="12"/>
I would assume, that the Slider would have a Height of 12. But it's still 3. The value of CanvasThickness is 12.
How do I repaint the CustomControl on PropertyChanged? I tried FrameworkPropertyMetadataOptions, but it does not affect the CustomControl.
Thanks in advance.
EDIT: If possible, the solution should also run in Silverlight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很简单:)
It's simple :)
Slider 模板中的
TemplateBinding
正在尝试解析模板化控件上名为CanvasThickness
的属性,该控件是一个Slider
,不是您的自定义控件。我一开始就不明白 Canvas 的意义。如果不确切知道您想要实现什么目标,就不可能说出来,但我怀疑您想要更接近此的东西:
即使如此,
Border
也具有硬编码的宽度和高度,这通常是一个坏主意。The
TemplateBinding
in your Slider template is attempting to resolve a property calledCanvasThickness
on the templated control, which is aSlider
, not your custom control.I don't understand the point of the
Canvas
in the first place. It's impossible to say without knowing exactly what you're trying to achieve, but I suspect you want something closer to this:Even then, the
Border
has hard-coded widths and heights which is generally a bad idea.