【发布时间】:2010-04-20 17:15:11
【问题描述】:
如何为我的自定义控件定义 TemplateBinding?
【问题讨论】:
标签: wpf custom-controls
如何为我的自定义控件定义 TemplateBinding?
【问题讨论】:
标签: wpf custom-controls
有点像这样......(顺便说一句,这个 xaml 是 WPF,而不是 silverlight——这有点不同)
<style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Green">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background={TemplateBinding Background}
</ControlTemplate>
</Setter.Value>
</Setter>
</style>
现在,一旦将此样式应用于对象,无论何时设置该对象的背景,模板都会使用 Background 属性(这是按钮控件上的属性),并将默认为您在样式(在本例中为绿色)
如果您想使用您的样式对象上不存在的属性,您必须派生自己的控件并将该属性添加为DependencyProperty 或使用INotifyPropertyChanged 接口。 Here 给你一个不错的解释。
【讨论】:
需要更多关于您正在尝试做什么的信息。可以使用以下 XAML 设置 TemplateBinding:
{TemplateBinding YourProperty}
或
{Binding RelativeSource={RelativeSource TemplatedParent}, Path=YourProperty}
【讨论】: