2011-12-03 78 views
2

我用下面的代碼創建了自定義ctextbox。但我無法爲此提供圓角邊框。如何在WPF中爲自定義TextBox控件指定CornerRadius?

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)}; 
    } 
    } 

請指導我這個嗎?

回答

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> 

希望這有助於

1

我想補充這是一個粘性上punker76的巨大反響:

如果你曾經想要修改.Net中當前可用的FrameworkElement對象的任何默認樣式,但有很多方法可以使用它,但我總是喜歡這個方便的工具:

Show Me The Template