Kiran Kumar
Kiran Kumar

Reputation: 59

How to make the Content in a Password Box in Xaml to be center?

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.

Upvotes: 0

Views: 2389

Answers (1)

Muster Station
Muster Station

Reputation: 514

The problem seems to lie with the PasswordBox Style. The root Border element doesn't have 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>

try adding an encapsulating Grid or modifying the Border's HorizontalAlignment it to something like this:

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

Then load your style as a StaticResource

Upvotes: 1

Related Questions