2010-02-18 33 views
2

HorizontalContentAlignment不应用到文本框....风格是不是在WPF应用到文本框

这里是样式定义:

<Style TargetType="TextBox"> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
</Style> 

,这里是文本框定义

<DockPanel LastChildFill="True"> 
    <TabControl DockPanel.Dock="Top" Name="tabControl1"> 
     <TabItem Header="TemplateItem"> 
      <DockPanel LastChildFill="True"> 
       <GroupBox Header="Salutation" DockPanel.Dock="Top"> 
        <StackPanel Orientation="Horizontal" Margin="0, 4, 0, 0"> 
         <TextBox Name="tbSalut" Width="300"/> 
         <TextBlock Text="->" Margin="4, 0, 4, 0"/> 
         <TextBox Name="tbSalutTransl" Width="371"/> 
        </StackPanel> 

以下是完整的示例:

<Window x:Class="WpfApplication1.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> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="BorderThickness" Value="10,1,1,1"/> 
      <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     </Style> 

    </Window.Resources> 

    <Grid> 
     <TextBox Height="23" Margin="12,51,12,0" Name="textBox1" VerticalAlignment="Top" /> 
    </Grid> 
</Window> 

回答

3

使用

<Setter Property="TextAlignment" Value="Center"/>

相反在风格

2

<Style TargetType="TextBox"> 

应该是:

<Style TargetType="{x:Type TextBox}"> 
+0

感谢的更喜欢,我已经尝试过,也不管用。 此外,我已将完整的源代码添加到帖子中。 其他属性适用于TextBox。 – Dmitry 2010-02-18 13:55:53