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