当用户向texbox输入一些文本时,我会创建一个搜索文本框,它将搜索他,因此我想知道该文本框的内容,现在我拥有:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight"
...
<TextBox Text="{Binding Search}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand Command="{Binding SearchCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
在viewModel上:
public ICommand SearchCommand { get; private set; }
public MyViewModel()
{
SearchCommand = new RelayCommand(SearchMethod);
}
void SearchMethod()
{
if(Search==null)
MessageBox.Show("Search text is null");
}
string search;
public string Search
{
get { return search; }
set
{
Set(() => Search, ref search, value);
RaisePropertyChanged("Search");
}
}
但是每次我在文本框中输入一些文本时,都会显示以下消息:Search text is null
答案 0 :(得分:0)
默认情况下,当 TextBox失去焦点时,对绑定进行评估。按下任何键都会触发您的事件。
要更改此行为,可以在Binding中设置UpdateSourceTrigger
:
<TextBox Text="{Binding Search, UpdateSourceTrigger=PropertyChanged}" />