gpt4 book ai didi

c# - 将 TextBlock 的可见性绑定(bind)到 TextBox

转载 作者:行者123 更新时间:2023-12-03 10:54:23 25 4
gpt4 key购买 nike

我正在尝试绑定(bind) VisibilityTextBlock基于他们选择的用户名是否可用。这是 TextBlock 的 XAML :

<TextBlock Grid.Row="5" Text="* Username already taken" Visibility="{Binding UsernameAvailable, Converter={StaticResource BoolToVis}}" Margin="5"/>

它绑定(bind)到的属性,以及触发的命令是:
public bool UsernameAvailable { get; set; }

#region RegisterCommand

private DelegateCommand _registerCommand;
public ICommand RegisterCommand
{
get
{
_registerCommand = new DelegateCommand(param => Register());
return _registerCommand;
}
}

private void Register()
{
if (IsPasswordValid())
{
var newUser = new User
{
FirstName = _firstName,
LastName = _lastName,
Username = _userName,
Password = _password //TODO: Hashing of password
};
using (var context = new WorkstreamContext())
{
var users = context.Set<User>();
users.Add(newUser);
context.SaveChanges();
}
}
else
{
UsernameAvailable = true; // TODO: Display TextBlock correctly
MessageBox.Show("Failed"); // TODO: Correctly show messages displaying what is incorrect with details
}
}

public bool IsPasswordValid()
{
return FirstName != string.Empty &&
LastName != string.Empty &&
UserName != string.Empty &&
Password.Any(char.IsUpper);
}

#endregion

MessageBox 显示,但是 TextBlock没有出现。如何确保 TextBlock当我检查注册方法中是否已使用用户名时显示?

最佳答案

您必须阅读 INotifyPropertyChanged,实现此接口(interface),然后将 UsernameAvailable 属性修改为:

private usernameAvailable

public bool UsernameAvailable
{
get
{
return usernameAvailable;
}
set
{
if (usernameAvailable != value)
{
usernameAvailable = value;
OnPropertyChanged(nameof(UsernameAvailable));
}
}
}

Here您可以找到 INotifyPropertyChanged 实现示例。

关于c# - 将 TextBlock 的可见性绑定(bind)到 TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45063423/

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