样式不适用于WPF中的TextBox

时间:2010-02-18 12:10:41

标签: wpf textbox wpf-controls styles

HorizontalContentAlignment未应用于文本框....

以下是样式定义:

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

这是TextBox定义

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

2 个答案:

答案 0 :(得分:3)

使用

  

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

而不是风格

答案 1 :(得分:2)

<Style TargetType="TextBox">

应该是:

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