2011-01-24 84 views
31

我不知道WPF,現在正在學習它。我正在尋找WPF中的圓角TextBox。所以,我搜索谷歌和發現了一張XAMLWPF圓角文本框

<!–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的初學者。

回答

51

在WPF中,您可以修改或重新創建控件的外觀。所以如果你的例子他們做了什麼,他們通過修改現有的TextBoxControlTemplate來改變文本框的外觀。所以,看到和探索的代碼段只需使用下面的代碼

<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> 

所以我們已經宣佈在窗口的資源部分的靜態資源,我們已經在Template屬性中使用的資源TextBoxBaseControlTemplate作爲Template="{StaticResource TextBoxBaseControlTemplate}"TextBox

模板自定義WPF控件只是參考,這個文件得到一個想法

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

19

@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的所有邊框具有圓角,將樣式放入您的窗口或應用程序的<Resources>

+0

非常感謝。自2008年以來,我從來沒有想過這件事!!!? – 2017-06-11 20:51:01

1

只需將BorderThicknessof文本框設置爲零,即可在文本框周圍添加邊框。

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

輸出如圖所示! Output!