2011-01-24 88 views
31

我不知道WPF,现在正在学习它。我正在寻找WPF中的圆角TextBox。所以,我搜索谷歌和发现了一张XAMLWPF圆角文本框

<!–Rounded Corner TextBoxes–> 
<ControlTemplate x:Key=”RoundTxtBoxBaseControlTemplate” TargetType=”{x:Type Control}”> 
<Border Background=”{TemplateBinding Background}” x:Name=”Bd” BorderBrush=”{TemplateBinding BorderBrush}” 
BorderThickness=”{TemplateBinding BorderThickness}” CornerRadius=”6″> 
<ScrollViewer x:Name=”PART_ContentHost”/> 
</Border> 
<ControlTemplate.Triggers> 
<Trigger Property=”IsEnabled” Value=”False”> 
<Setter Property=”Background” Value=”{DynamicResource {x:Static SystemColors.ControlBrushKey}}” TargetName=”Bd”/> 
<Setter Property=”Foreground” Value=”{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}”/> 
</Trigger> 
<Trigger Property=”Width” Value=”Auto”> 
<Setter Property=”MinWidth” Value=”100″/> 
</Trigger> 
<Trigger Property=”Height” Value=”Auto”> 
<Setter Property=”MinHeight” Value=”20″/> 
</Trigger> 
</ControlTemplate.Triggers> 
</ControlTemplate> 

所以,请告诉我在哪里粘贴此XAML。请详细帮助我。我是WPF的初学者。

回答

51

在WPF中,您可以修改或重新创建控件的外观。所以如果你的例子他们做了什么,他们通过修改现有的TextBoxControlTemplate来改变文本框的外观。所以,看到和探索的代码段只需使用下面的代码

<Window x:Class="WpfApplication4.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="300"> 
<Window.Resources> 
    <ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}"> 
     <Border Background="{TemplateBinding Background}" 
       x:Name="Bd" BorderBrush="Black" 
       BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10"> 
      <ScrollViewer x:Name="PART_ContentHost"/> 
     </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/> 
       <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
      </Trigger> 
      <Trigger Property="Width" Value="Auto"> 
       <Setter Property="MinWidth" Value="100"/> 
      </Trigger> 
      <Trigger Property="Height" Value="Auto"> 
       <Setter Property="MinHeight" Value="20"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
</Window.Resources> 
<Grid> 
    <TextBox Template="{StaticResource TextBoxBaseControlTemplate}" Height="25" Margin="5"></TextBox> 
</Grid> 

所以我们已经宣布在窗口的资源部分的静态资源,我们已经在Template属性中使用的资源TextBoxBaseControlTemplate作为Template="{StaticResource TextBoxBaseControlTemplate}"TextBox

模板自定义WPF控件只是参考,这个文件得到一个想法

http://msdn.microsoft.com/en-us/magazine/cc163497.aspx

19

@Smolla曾在他的@Daniel Casserly的答复意见更好的答案:

<TextBox Text="TextBox with CornerRadius"> 
    <TextBox.Resources> 
    <Style TargetType="{x:Type Border}"> 
     <Setter Property="CornerRadius" Value="3"/> 
    </Style> 
    </TextBox.Resources> 
</TextBox> 

如果您希望TextBoxes和ListBoxes的所有边框具有圆角,将样式放入您的窗口或应用程序的<Resources>

+0

非常感谢。自2008年以来,我从来没有想过这件事!!!? – 2017-06-11 20:51:01

1

只需将BorderThicknessof文本框设置为零,即可在文本框周围添加边框。

<Border BorderThickness="1" BorderBrush="Black" CornerRadius="10" Padding="2" 
     HorizontalAlignment="Center" VerticalAlignment="Center"> 
     <TextBox Text="Hello ! " BorderThickness="0"/> 
</Border> 

输出如图所示! Output!