WPF圆角文本框

Tho*_*mas 38 wpf textbox rounded-corners

我不知道WPF,我现在正在学习它.我在寻找TextBoxWPF的圆角.所以我搜索了谷歌并找到了一条XAML:

 <!–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>
Run Code Online (Sandbox Code Playgroud)

所以请告诉我在哪里贴这个XAML.请详细帮我.我是WPF的初学者.

Kis*_*mar 59

在WPF中,您可以修改或重新创建控件的外观.因此,如果您的示例他们所做的是他们通过修改ControlTemplate现有的TextBox来改变它的外观TextBox.因此,要查看和探索这段代码,只需使用下面的代码即可

<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>
Run Code Online (Sandbox Code Playgroud)

因此,我们宣布在窗口的资源部分的静态资源,我们已经在使用的资源TextBoxBaseControlTemplate Template的财产TextBox作为 Template="{StaticResource TextBoxBaseControlTemplate}".

自定义WPF控件的模板只需参考此文档即可获得一个想法

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


che*_*ica 45

@Smolla在评论@Daniel Casserly的答案时得到了更好的答案:

<TextBox Text="TextBox with CornerRadius">
  <TextBox.Resources>
    <Style TargetType="{x:Type Border}">
      <Setter Property="CornerRadius" Value="3"/>
    </Style>
  </TextBox.Resources>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

如果您希望TextBoxes和ListBoxes的所有边框都有圆角,请将样式放入Window或App中<Resources>.

  • 这个答案要好得多,恕我直言。接受的答案是对 WPF 概念的一个很好的解释,但这个答案是关于“优雅地完成它”。 (4认同)

Cla*_*ton 13

您可以使用以下样式将所有文本框更改为具有圆角:

<Style TargetType="{x:Type TextBox}">
  <Style.Resources>
    <Style TargetType="{x:Type Border}">
      <Setter Property="CornerRadius" Value="3" />
    </Style>
  </Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)

受到以下答案的启发:https : //stackoverflow.com/a/13858357/3387453


Thu*_*der 6

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

 <Border BorderThickness="1" BorderBrush="Black" CornerRadius="10" Padding="2"
        HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBox Text="Hello ! " BorderThickness="0"/>
 </Border>
Run Code Online (Sandbox Code Playgroud)

输出如图所示! 输出!