繁体   English   中英

如何在WPF中为自定义TextBox控件指定CornerRadius?

[英]How to Specify CornerRadius for custom TextBox control in WPF?

我用以下代码创建了自定义ctextbox。 但我无法为此提供圆角边界。

public class FilteredTextBox : TextBox
{


    public FilteredTextBox()
        : base()
    {
        IsNumeric = false;
        IsRegex = false;
        IsRequired = false;
        ErrorMsg = "";
        RegexText = "";
        HorizontalAlignment = HorizontalAlignment.Stretch;
        Margin = new Thickness(0);
        BorderThickness = new Thickness(1);
        var border = new Border {CornerRadius = new CornerRadius(4)};
     }
   }

请指导我这个?

您可以使用自定义TextBox的样式执行此操作:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>

    <Grid.Resources>
      <Style x:Key="CustomTextBoxStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
              <Border
                CornerRadius="4"
                Padding="2"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="1" >
                <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
              </Border>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Grid.Resources>

    <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
      <CustomTextBox Style="{StaticResource CustomTextBoxStyle}" Text="TextBox with CornerRadius" BorderBrush="Black" />
    </Grid>

  </Grid>
</Page>

希望这可以帮助

我想将此作为对punker76的响应的补充:

如果你想修改.Net中可用的当前FrameworkElement对象的任何默认样式,有很多方法可以解决它,但我总是喜欢这个方便的工具:

告诉我模板

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM