我创建了一个自定义 WPF 用户控件。问题是,有时我需要 BorderThickness 为 0,有时需要 BorderThickness 为 1。
<UserControl ...>
<clay:TextBox x:Name="ClayTextBox"
BorderThickness="1" >
</clay:TextBox>
</UserControl>
如果我像这样在 xaml 文档中使用控件:
<clay:TextBox x:Name="ClayTextBox"
BorderThickness="0" >
</clay:TextBox>
...边框始终为 1。我该如何解决?
最佳答案
在你的自定义控件模板样式中,你应该将父容器控件设置为边框,然后使用模板绑定(bind)来绑定(bind)边框粗细。在这里,我假设您的 CustomControl 继承了一个将 BorderThickness 作为属性的控件。
<ControlTemplate TargetType="{x:Type clay:TextBox}">
<Border BorderThickness="{TemplateBinding BorderThickness}">
//Remaining xaml that makes up your custom control.
</Border>
</ControlTemplate>
关于c# - 自定义控件 BorderThickness,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28189498/