除非选中,否则数据绑定文本框不会更新 [英] databound textbox not updating unless selected
问题描述
我有一个静态单例类,该类具有用于将文本框数据绑定到的属性。
I have a static singleton class with properties I use to databind textboxes to.
using System.ComponentModel;
namespace Masca
{
public class logged : INotifyPropertyChanged
{
public static logged instance = new logged();
public static logged Instance
{
get { return instance; }
}
private string alisa;
public string aliasname
{
get { return alisa; }
set
{
alisa = value;
RaisePropertyChanged("aliasname");
}
}
private string mail;
public string emailadd
{
get { return mail; }
set
{
mail = value;
RaisePropertyChanged("emailadd");
}
}
private void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged (this, new PropertyChangedEventArgs(prop));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
这是属性访问方法:
loggedin.Instance.emailadd = "email.text";
这是我放置在我希望访问该属性的所有其他页面的Initialize组件中的数据上下文:
This is the datacontext I place in the initialize components of all other pages I wish to access the property:
DataContext = loggedin.Instance;
这是绑定文本框的XAML代码
and this is the XAML code for a bound TextBox
<TextBox x:Name="email" Text="{Binding emailadd}" Height="19" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11" Margin="134,417,0,0"/>
<TextBox x:Name="mail" Text="{Binding emailadd}" Height="19" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11" Margin="134,435,0,0"/>
问题是,如果我在email.text中键入内容,mail.text将仅反映
The problem is that if I type something into email.text, mail.text will only reflect what is in email.text once i have clicked in mail.text.
非常感谢您的帮助。
推荐答案
尝试设置 UpdateSouceTrigger
:
<TextBox x:Name="email" Text="{Binding emailadd,
UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}" />
请注意, UpdateSourceTrigger
的默认值 TextBox.Text
是 LostFocus
,而对于其他许多属性,它是 PropertyChanged
。
Note that the default value of UpdateSourceTrigger
for a TextBox.Text
is LostFocus
, while for many other properties, it is PropertyChanged
.
这篇关于除非选中,否则数据绑定文本框不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!