0

I am trying to make the content, in a Password box to align in center , when we type something in that, but I am not able to do it.I have tried this link

this is the code:

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

This is for a windows store app. But not sure how they are doing.I would be grateful if anyone could answer this.

4

1 回答 1

1

问题似乎在于PasswordBox Style。根边框元素没有 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>

尝试添加一个封装 Grid 或将边框的 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

于 2014-08-01T18:57:03.743 回答