我有一个绑定此属性的文本框:
private string _username;
public string Username
{
get { return _firstName + '_' + _lastName + GenerateRandomNo(); }
set
{
_username = value;
NotifyOfPropertyChange("Username");
}
}
我要在这里实现的是获取名字+姓氏(都绑定到文本框)+生成一个随机数,并将其放入绑定了“用户名”(上面)的文本框中。
我得到的输出:
_1548
(基本上除了名字和姓氏之外的所有东西)
XAML:
<TextBox Text="{Binding Username,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource MaterialDesignFloatingHintTextBox}" >
答案 0 :(得分:2)
我想您需要用NotifyPropertyChange
的名字和姓氏来更新Username
。像这样:
private string _firstname;
public string Firstname
{
get { return _firstname; }
set
{
_firstname = value;
NotifyOfPropertyChange("Firstname");
NotifyOfPropertyChange("Username");
}
}
private string _lastname;
public string Lastname
{
get { return _lastname; }
set
{
_lastname = value;
NotifyOfPropertyChange("Lastname");
NotifyOfPropertyChange("Username");
}
}
然后:
public string Username
{
get { return Firstname + '_' + Lastname + GenerateRandomNo(); }
}