最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - WPF ToggleButton for Password Visibility Not Switching Images Correctly - Stack Overflow

programmeradmin1浏览0评论

I'm working on a WPF project where I have a ToggleButton to toggle the visibility of a password. When the button is clicked, it should switch between showing the password and hiding it, using Visibility.png and VisibilityOff.png respectively. However, I'm encountering a couple of issues:

  1. When I click on the Visibility.png, the password is shown, but the image does not switch to VisibilityOff.png.

It displays the Visibility.png icon when I open the app

When I start to enter it, it's hidden.

When I click the button with the image Visibility.png, it shows the password, but the button disappears, and I can no longer see the image. Additionally, I cannot switch back to the Visibility.png button.

  1. The buttonto switch back toVisibility.png` doesn't seem to work, and I don't see any button.

Here is the relevant part of my XAML code:

<Grid Margin="0,0,0,20">
   <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Border CornerRadius="5" BorderBrush="#CCCCCC" BorderThickness="1">
        <PasswordBox Name="PasswordBox" 
                     FontSize="18"
                     BorderBrush="Black"
                     BorderThickness="2"
                     Padding="10" 
                     Background="#FFFFFF" 
                     GotFocus="PasswordBox_GotFocus" 
                     LostFocus="PasswordBox_LostFocus" 
                     PasswordChanged="PasswordBox_PasswordChanged"/>
    </Border>

    <TextBlock Text="Password" 
              Foreground="#AAAAAA" 
              Padding="7"
              FontSize="18"
              VerticalAlignment="Center" 
              Margin="10,0,0,0" 
              IsHitTestVisible="False" 
              Name="PasswordPlaceholder"/>

    <!-- Password Visibility Toggle -->
    <ToggleButton x:Name="PasswordToggleButton" 
                  Width="30" 
                  Height="30" 
                  VerticalAlignment="Center" 
                  HorizontalAlignment="Right" 
                  Margin="0,0,10,0" 
                  Cursor="Hand"
                  Checked="PasswordToggleButton_Checked"
                  Unchecked="PasswordToggleButton_Unchecked">
        <ToggleButton.Template>
            <ControlTemplate TargetType="ToggleButton">
                <Grid>
                    <Image x:Name="PasswordEyeIcon" 
                           Source="pack://application:,,,/img/Visibility.png" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="PasswordEyeIcon" Property="Source" Value="pack://application:,,,/img/VisibilityOff.png" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ToggleButton.Template>
    </ToggleButton>

    <!-- TextBox for password visibility -->
    <TextBox Name="PasswordTextBox" 
             FontSize="18" 
             BorderBrush="Black" 
             BorderThickness="2" 
             Padding="10" 
             Background="#FFFFFF" 
             Visibility="Hidden" />
</Grid>

And here is the relevant part of my code-behind:

private void PasswordToggleButton_Checked(object sender, RoutedEventArgs e)
{
    // Show the password in clear text
    PasswordBox.Visibility = Visibility.Hidden;
    PasswordTextBox.Visibility = Visibility.Visible;
    PasswordTextBox.Text = PasswordBox.Password; // Sync the password
}

private void PasswordToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
    // Hide the password and show the PasswordBox
    PasswordBox.Visibility = Visibility.Visible;
    PasswordTextBox.Visibility = Visibility.Hidden;
    PasswordBox.Password = PasswordTextBox.Text; // Sync the password back
}

What could be causing the ToggleButton to not switch images correctly and the button to not appear as expected? Any help would be greatly appreciated!

  1. I have verified that the image paths are correct
  2. I verified for my VisibilityOff.png Build Action to be a Resource
  3. I placed in the :
<Image x:Name="PasswordEyeIcon"Source="pack://application:,,,/img/VisibilityOff.png" /> 

and i can see the image VisibilityOff.png in my project when I run it

I'm working on a WPF project where I have a ToggleButton to toggle the visibility of a password. When the button is clicked, it should switch between showing the password and hiding it, using Visibility.png and VisibilityOff.png respectively. However, I'm encountering a couple of issues:

  1. When I click on the Visibility.png, the password is shown, but the image does not switch to VisibilityOff.png.

It displays the Visibility.png icon when I open the app

When I start to enter it, it's hidden.

When I click the button with the image Visibility.png, it shows the password, but the button disappears, and I can no longer see the image. Additionally, I cannot switch back to the Visibility.png button.

  1. The buttonto switch back toVisibility.png` doesn't seem to work, and I don't see any button.

Here is the relevant part of my XAML code:

<Grid Margin="0,0,0,20">
   <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Border CornerRadius="5" BorderBrush="#CCCCCC" BorderThickness="1">
        <PasswordBox Name="PasswordBox" 
                     FontSize="18"
                     BorderBrush="Black"
                     BorderThickness="2"
                     Padding="10" 
                     Background="#FFFFFF" 
                     GotFocus="PasswordBox_GotFocus" 
                     LostFocus="PasswordBox_LostFocus" 
                     PasswordChanged="PasswordBox_PasswordChanged"/>
    </Border>

    <TextBlock Text="Password" 
              Foreground="#AAAAAA" 
              Padding="7"
              FontSize="18"
              VerticalAlignment="Center" 
              Margin="10,0,0,0" 
              IsHitTestVisible="False" 
              Name="PasswordPlaceholder"/>

    <!-- Password Visibility Toggle -->
    <ToggleButton x:Name="PasswordToggleButton" 
                  Width="30" 
                  Height="30" 
                  VerticalAlignment="Center" 
                  HorizontalAlignment="Right" 
                  Margin="0,0,10,0" 
                  Cursor="Hand"
                  Checked="PasswordToggleButton_Checked"
                  Unchecked="PasswordToggleButton_Unchecked">
        <ToggleButton.Template>
            <ControlTemplate TargetType="ToggleButton">
                <Grid>
                    <Image x:Name="PasswordEyeIcon" 
                           Source="pack://application:,,,/img/Visibility.png" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="PasswordEyeIcon" Property="Source" Value="pack://application:,,,/img/VisibilityOff.png" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ToggleButton.Template>
    </ToggleButton>

    <!-- TextBox for password visibility -->
    <TextBox Name="PasswordTextBox" 
             FontSize="18" 
             BorderBrush="Black" 
             BorderThickness="2" 
             Padding="10" 
             Background="#FFFFFF" 
             Visibility="Hidden" />
</Grid>

And here is the relevant part of my code-behind:

private void PasswordToggleButton_Checked(object sender, RoutedEventArgs e)
{
    // Show the password in clear text
    PasswordBox.Visibility = Visibility.Hidden;
    PasswordTextBox.Visibility = Visibility.Visible;
    PasswordTextBox.Text = PasswordBox.Password; // Sync the password
}

private void PasswordToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
    // Hide the password and show the PasswordBox
    PasswordBox.Visibility = Visibility.Visible;
    PasswordTextBox.Visibility = Visibility.Hidden;
    PasswordBox.Password = PasswordTextBox.Text; // Sync the password back
}

What could be causing the ToggleButton to not switch images correctly and the button to not appear as expected? Any help would be greatly appreciated!

  1. I have verified that the image paths are correct
  2. I verified for my VisibilityOff.png Build Action to be a Resource
  3. I placed in the :
<Image x:Name="PasswordEyeIcon"Source="pack://application:,,,/img/VisibilityOff.png" /> 

and i can see the image VisibilityOff.png in my project when I run it

Share Improve this question asked Mar 13 at 6:20 AlexAlex 111 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You fot to set the Grid.Column property on the ToggleButton:

<ToggleButton x:Name="PasswordToggleButton" Grid.Column="1" ...>

As a note, in XAML you can omit the pack://application:,,, prefix of the image URIs:

<ControlTemplate TargetType="ToggleButton">
    <Grid>
        <Image x:Name="PasswordEyeIcon" Source="/img/Visibility.png" />
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <Setter TargetName="PasswordEyeIcon"
                    Property="Source" Value="/img/VisibilityOff.png" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Besides that, you may perhaps want to set the Visibilities of the PasswordBox and the TextBox by DataTriggers on the ToggleButton's IsChecked property, instead of setting them in code behind.

It is also unclear why you don't toggle the Visibility of the Border parent of the PasswordBox instead of that of the PasswordBox.

发布评论

评论列表(0)

  1. 暂无评论