Wpf 如何使用TemplateBinding继承控件的所有属性
我正在尝试制作自己版本的控件,比如说Wpf 如何使用TemplateBinding继承控件的所有属性,wpf,xaml,datatemplate,Wpf,Xaml,Datatemplate,我正在尝试制作自己版本的控件,比如说文本框,我想在其中添加一些自定义UI。我想从这个类继承,这样用户仍然可以像调用普通文本框一样调用我的特殊文本框。我有以下资料: // MySpecialTextBox.cs public class MySpecialTextBox : TextBox { static MySpecialTextBox () { } /* Some special properties */ } 在XAML中: <Style Tar
文本框
,我想在其中添加一些自定义UI。我想从这个类继承,这样用户仍然可以像调用普通文本框一样调用我的特殊文本框。我有以下资料:
// MySpecialTextBox.cs
public class MySpecialTextBox : TextBox
{
static MySpecialTextBox () { }
/* Some special properties */
}
在XAML中:
<Style TargetType="{x:Type MySpecialTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MySpecialTextBox}">
<StackPanel>
<Rectangle Width="100" Height="3" Fill="Pink" /> <!-- My own fancy lay-out -->
<TextBox Text="{TemplateBinding Text}"/> <!-- This text box should 'inherit' ALL the properties from the caller -->
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
列出所有属性,但这感觉效率极低。有没有一种方法可以一次绑定到整个父对象?将另一个
TextBox
元素放置在另一个TextBox
的模板中不是一个好主意。当覆盖控件的模板时,应该始终编辑其默认模板
因此,在文本框的默认模板中
可以找到以下元素:
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
TemplateBinding
是标准Binding
的优化形式,大致相当于使用Binding
,将RelativeSource
属性设置为RelativeSource.TemplatedParent
。因此,与标准Binding
s类似,只能对单个属性进行设置。从MSDN上的页面:
您可以在template中使用TemplateBinding
绑定到应用模板的控件上的值
从MSDN上的页面:
<代码>模板绑定只有一个可设置的属性
这意味着我们不能为它提供多个属性名称,因此它只能使用单个属性。不幸的是,没有一种方便的
SuperTemplateBinding
可以影响所有属性,但即使有,我也无法想象任何简单的方法来映射哪些属性应该绑定到哪些模板属性。因此,答案是“不,没有从模板化父控件继承属性值的快捷方式”。不,没有。。。您必须设置要使用的每个属性。真的吗-(好吧,我猜否定的答案也是答案。也没有解决办法吗?虽然你创建了自己的TextBox类,但它与普通情况没有区别。你覆盖默认模板,为了从父属性中获取任何值,你需要做TemplateBinding。你能不能详细说明一下?我不完全理解了解此代码所在的位置和/或可以编辑的位置。因为在文本框样式中没有名为Border
的属性可以编辑(只有BorderThickness
和BorderBrush
)。在模板中,您应该使用具有特殊名称“PART\u ContentHost”的ScrollViewer
。请参阅我的编辑。TemplateBinding
和RelativeSource.TemplatedParent
不相同。首先在编译时解析,然后在运行时解析。
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
<Style TargetType="{x:Type my:MySpecialTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type my:MySpecialTextBox}">
<StackPanel>
<Rectangle Width="100" Height="3" Fill="Pink" />
<!-- My own fancy lay-out -->
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>