【问题标题】:WPF rounded corner textboxWPF圆角文本框
【发布时间】:2011-01-24 07:57:45
【问题描述】:

我不知道 WPF,现在正在学习它。我在 WPF 中寻找圆角TextBox。于是搜了谷歌,找到了一段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>

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

【问题讨论】:

    标签: wpf textbox rounded-corners


    【解决方案1】:

    @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 的所有边框都有圆角,请将样式放入您的 Window 或 App 的&lt;Resources&gt;。

    【讨论】:

    • 这个答案要好得多,恕我直言。公认的答案是对 WPF 概念的一个很好的解释,但这个答案都是关于“优雅地完成它”。
    【解决方案2】:

    在 WPF 中,您可以修改或重新创建控件的外观。因此,如果您的示例他们所做的是他们通过修改现有TextBox 的ControlTemplate 来改变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>
    

    所以我们在 Window 的 Resource 部分声明了一个静态资源,并且我们将 TextBox 的 Template 属性中的 Resource TextBoxBaseControlTemplate 用作 Template="{StaticResource TextBoxBaseControlTemplate}" 。

    自定义 WPF 控件的模板只是参考这个文档来获得一个想法

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      【解决方案4】:

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

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

      输出如图所示!

      【讨论】:

        【解决方案5】:

        这个问题在 msdn 上得到了很好的讨论: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/549775ed-1c2a-4911-9078-d9c724294fb3/

        在那里尝试解决方案,它们非常详细,当然也足够详细,让您知道将代码放在哪里。

        【讨论】:

        • 此线程的最后一个我发现对简单的解决方案很有帮助:&lt;TextBox Text="TextBox with CornerRadius"&gt; &lt;TextBox.Resources&gt; &lt;Style TargetType="{x:Type Border}"&gt; &lt;Setter Property="CornerRadius" Value="3"/&gt; &lt;/Style&gt; &lt;/TextBox.Resources&gt; &lt;/TextBox&gt;
        【解决方案6】:

        您可以使用附加属性来设置TextBox 边框半径(同样适用于按钮)。

        为附加属性创建类

        public class CornerRadiusSetter
        {
            public static CornerRadius GetCornerRadius(DependencyObject obj) => (CornerRadius)obj.GetValue(CornerRadiusProperty);
        
            public static void SetCornerRadius(DependencyObject obj, CornerRadius value) => obj.SetValue(CornerRadiusProperty, value);
        
            public static readonly DependencyProperty CornerRadiusProperty =
                DependencyProperty.RegisterAttached(nameof(Border.CornerRadius), typeof(CornerRadius),
                    typeof(CornerRadiusSetter), new UIPropertyMetadata(new CornerRadius(), CornerRadiusChangedCallback));
        
            public static void CornerRadiusChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
            {
                Control control = sender as Control;
        
                if (control == null) return;
        
                control.Loaded -= Control_Loaded;
                control.Loaded += Control_Loaded;
            }
        
            private static void Control_Loaded(object sender, EventArgs e)
            {
                Control control = sender as Control;
        
                if (control == null || control.Template == null) return;
        
                control.ApplyTemplate();
        
                Border border = control.Template.FindName("border", control) as Border;
        
                if (border == null) return;
        
                border.CornerRadius = GetCornerRadius(control);
            }
        }
        

        然后您可以使用附加属性语法来设置多个文本框的样式,而不会出现样式重复:

        <TextBox local:CornerRadiusSetter.CornerRadius="10" />
        <TextBox local:CornerRadiusSetter.CornerRadius="5, 0, 0, 5" />
        <TextBox local:CornerRadiusSetter.CornerRadius="10, 4, 18, 7" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-12-30
          • 2021-12-29
          • 1970-01-01
          • 1970-01-01
          • 2010-11-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多