1

我在 wpf 中创建了一个文本框、一个密码框和一个切换按钮。我需要,当我检查按钮时,密码是可见的,当按钮未选中时,字符显示为项目符号。我的问题是,当我运行应用程序时,该按钮未选中(这意味着隐藏密码),但仍然显示密码。就在我检查按钮之后,一切都按我想要的方式工作。如何从头开始绑定?不仅在我检查过之后

XML 代码:

   <ToggleButton  Name="toggle1" Height="40" Padding="0" Width="56" Canvas.Left="131" Canvas.Top="0" BorderBrush="{x:Null}" IsChecked="{Binding ShowPassword}">

代码:

public bool IsPasswordVisible
    {
        get { return _IsPasswordVisible; }
        set
        {
            if (_IsPasswordVisible == value)
                return;
            _IsPasswordVisible = value;
            if(IsPasswordVisible)
            {
                passwordBox1.Visibility = System.Windows.Visibility.Collapsed;
                textbox1.Visibility = System.Windows.Visibility.Visible;
                textbox1.EditValue = passwordBoxEdit1.Password;
            }else{
                passwordBox1.Visibility = System.Windows.Visibility.Visible;
                textbox1.Visibility = System.Windows.Visibility.Collapsed;
                passwordBox1.Password = textEdit1.Text;
            }
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("ShowPassword"));
        }
    }       

    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;

    }

    public event PropertyChangedEventHandler PropertyChanged;

    #region OnPropertyChanged
    /// <summary>
    /// Triggers the PropertyChanged event.
    /// </summary>
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
4

4 回答 4

2

看到 datacontext 后将 IsPasswordVisible 设置为 false

this.DataContext = this;
IsPasswordVisible = false;

从 IsPasswordVisible 设置器中删除以下代码

if (_IsPasswordVisible == value)
                return;
于 2013-09-18T09:00:56.000 回答
2

您可以仅通过绑定和转换器使用 xaml 来完成大部分工作。

例子:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <BoolToVisibilyConverter x:Key="boolVisibilityConverter"/>
    <BoolToVisibilyOppositeConverter x:Key="boolVisibilityOppsiteConverter"/>
</Window.Resources>
<Grid x:Name="myGrid" Margin="5" Width="500">
    <StackPanel>
        <TextBox Visibility="{Binding ShowPassword, Converter={StaticResource boolVisibilityOppsiteConverter}}"/>
        <PasswordBox Visibility="{Binding ShowPassword, Converter={StaticResource boolVisibilityConverter}}"/>
    </StackPanel>
</Grid>

转换器

class BoolToVisibilityConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return bool.Parse(value.ToString()) == true ? Visibility.Visible : Visibility.Collapsed; 
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

class BoolToVisibilityOppositeConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return bool.Parse(value.ToString()) == false ? Visibility.Visible : Visibility.Collapsed; 
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

然后,切换按钮应该只绑定到ShowPassword属性。

设置时ShowPassword,将文本从文本框复制到密码框,反之亦然。

public bool ShowPassword
{
    set
    {  
       if (_IsPasswordVisible == value)
            return;
        _IsPasswordVisible = value;

       if (_IsPasswordVisible = true)
          textbox1.EditValue = passwordBoxEdit1.Password;
       else
          passwordBox1.Password = textEdit1.Text;

       if (PropertyChanged != null)
           PropertyChanged(this, new PropertyChangedEventArgs("ShowPassword"));
    }

这样,TextBox可见PasswordBox性就与ShowPassword属性绑定了。因为它的默认值为 false 并且他们从那里设置了初始可见性,所以这应该可以解决您的问题

于 2013-09-18T08:27:21.310 回答
1

这只是因为在切换按钮中 IsChecked 的默认值为空。

public Nullable<bool> IsChecked { get; set; }
于 2013-09-18T08:18:11.443 回答
1

您可以尝试将密码框设置为默认折叠吗?其他一切都应该正常工作。

于 2013-09-18T08:26:09.200 回答