[英]Custom Control binding property
I have created a custom control but I can't bind a property to the content like this : 我已经创建了一个自定义控件但我不能将属性绑定到这样的内容:
<Style TargetType="control:Pie">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="control:Pie">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentControl Content="{Binding Path=Test}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And in my control 在我的掌控之中
public string Test
{
get { return (string)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register(
"Test",
typeof(string),
typeof(Pie),
new PropertyMetadata(null));
When I create the control a set a string in the Test property but nothing appears in the view... 当我创建控件时,在Test属性中设置一个字符串,但视图中没有任何内容...
TemplateBinding
is used to bind to the templated control and therefore only works in ControlTemplate
. TemplateBinding
用于绑定到模板化控件,因此仅适用于ControlTemplate
。 Just 2 Lines above you already used TemplateBinding for Background, BorderBrush and BorderThickness. 只有2行以上你已经使用了TemplateBinding for Background,BorderBrush和BorderThickness。
Binding
on the otherhand binds to the DataContext
, which is an object used to take "business" related data from the user. Binding
在otherhand结合到DataContext
,它是用于从用户采取“业务”相关的数据的对象。 It should be independant of how your controls works and 它应该与控件的工作方式无关
As a rule of thumb: If you use a normal binding in a ControlTemplate
, which doesn't have a RelativeSource
, ElementName
or Source
set, it usually should not appear in a ControlTemplate or Style. 根据经验:如果在
ControlTemplate
使用普通绑定(没有RelativeSource
, ElementName
或Source
集),它通常不应出现在ControlTemplate或Style中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.