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

VIJ*_*JAY 2 c# wpf user-controls styles textbox

我用以下代码创建了自定义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)};
     }
   }
Run Code Online (Sandbox Code Playgroud)

请指导我这个?

pun*_*r76 7

您可以使用自定义样式执行此操作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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助