Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义控件边框厚度_C#_Wpf_Textbox_User Controls_Border - Fatal编程技术网

C# 自定义控件边框厚度

C# 自定义控件边框厚度,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

我已经创建了一个自定义WPF用户控件。问题是,有时需要边界厚度为0,有时需要边界厚度为1

<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。