gpt4 book ai didi

c# - 获取已检查的事件数据绑定(bind) wpf

转载 作者:太空宇宙 更新时间:2023-11-03 19:13:23 25 4
gpt4 key购买 nike

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

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

最佳答案

在看到数据上下文后将 IsPasswordVisible 设置为 false

this.DataContext = this;
IsPasswordVisible = false;

从 IsPasswordVisible setter 中删除以下代码

if (_IsPasswordVisible == value)
return;

关于c# - 获取已检查的事件数据绑定(bind) wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18867049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com