C# 自定义控件边框厚度
我已经创建了一个自定义WPF用户控件。问题是,有时需要边界厚度为0,有时需要边界厚度为1C# 自定义控件边框厚度,c#,wpf,textbox,user-controls,border,C#,Wpf,Textbox,User Controls,Border,我已经创建了一个自定义WPF用户控件。问题是,有时需要边界厚度为0,有时需要边界厚度为1 <UserControl ...> <clay:TextBox x:Name="ClayTextBox" BorderThickness="1" > </clay:TextBox> </UserControl> 如果我在xaml文档中使用控件,如下所示: <clay:TextBox x:Name="ClayTe
<UserControl ...>
<clay:TextBox x:Name="ClayTextBox"
BorderThickness="1" >
</clay:TextBox>
</UserControl>
如果我在xaml文档中使用控件,如下所示:
<clay:TextBox x:Name="ClayTextBox"
BorderThickness="0" >
</clay:TextBox>
<UserControl x:Class="UseRcontrolWithProperty.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" x:Name="this"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Border BorderThickness="{Binding ElementName=this, Path=BorderThickness}"></Border>
</Grid>
</UserControl>
。。。边界始终为1。如何解决此问题?让边框将其BorderThickness属性绑定到UserControls属性,如下所示:
<clay:TextBox x:Name="ClayTextBox"
BorderThickness="0" >
</clay:TextBox>
<UserControl x:Class="UseRcontrolWithProperty.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" x:Name="this"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Border BorderThickness="{Binding ElementName=this, Path=BorderThickness}"></Border>
</Grid>
</UserControl>
这样,更改UserControl上的边界笔刷将更改内部边界的边界笔刷 在自定义控件模板样式中,应将父容器控件设置为边框,然后使用模板绑定绑定边框厚度。这里我假设CustomControl继承了一个属性为BorderThickness的控件
<ControlTemplate TargetType="{x:Type clay:TextBox}">
<Border BorderThickness="{TemplateBinding BorderThickness}">
//Remaining xaml that makes up your custom control.
</Border>
</ControlTemplate>
//组成自定义控件的剩余xaml。