如何使Xaml中的密码框中的内容成为中心?

时间:2014-08-01 16:18:47

标签: xaml passwordbox

我正在尝试制作内容,在密码框中以中心对齐,当我们输入内容时,但我无法执行此操作。我已尝试过此link

这是代码:

<PasswordBox Grid.Row="4" Password="{Binding Password,Mode=TwoWay}" BorderBrush="Transparent" HorizontalAlignment="Center" Margin="0" FontSize="25" HorizontalContentAlignment="Center"  VerticalContentAlignment="Center"  VerticalAlignment="Center" Width="550" Height="90" PlaceholderText="password" >
    <PasswordBox.Background>
        <ImageBrush  ImageSource="/Assets/Login/text-field.png"   AlignmentY="Bottom"></ImageBrush>
    </PasswordBox.Background>
</PasswordBox>

这适用于Windows应用商店。 但不确定他们是怎么做的。如果有人能回答这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题似乎在于PasswordBox Style。根Border元素没有TemplateBindings。

  <ControlTemplate TargetType="{x:Type PasswordBox}">
    <Border x:Name="Border"
            CornerRadius="2"
            Padding="2"
            BorderThickness="1">
      <Border.Background>
        <SolidColorBrush Color="{DynamicResource ControlLightColor}" />
      </Border.Background>
      <Border.BorderBrush>
        <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
      </Border.BorderBrush>
      <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
          <VisualState x:Name="Normal" />
          <VisualState x:Name="Disabled" />
          <VisualState x:Name="MouseOver" />
        </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>
      <ScrollViewer x:Name="PART_ContentHost" />
    </Border>
  </ControlTemplate>

尝试添加封装网格或修改Border的Horizo​​ntalAlignment它是这样的:

  <ControlTemplate TargetType="{x:Type PasswordBox}">
   <Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">
    <Border x:Name="Border"
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
            CornerRadius="2"
            Padding="2"
            BorderThickness="1">
      <Border.Background>
        <SolidColorBrush Color="{DynamicResource ControlLightColor}" />
      </Border.Background>
      <Border.BorderBrush>
        <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
      </Border.BorderBrush>
      <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
          <VisualState x:Name="Normal" />
          <VisualState x:Name="Disabled" />
          <VisualState x:Name="MouseOver" />
        </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>
      <ScrollViewer x:Name="PART_ContentHost" />
    </Border>
   </Grid>
  </ControlTemplate>

然后将您的样式加载为StaticResource