我正在实现一个基本的搜索文本框 - >按钮,但我也希望让用户能够按下Enter/Return键键盘/键盘按照How to enable enter button on keyboard on wp7?ViewModel中的RelayCommand:调用无法访问ViewModel中的绑定UI数据
当RelayCommand被调用时,我只是得到一个空值的搜索文本框的内容。
我正在使用MVVM Light。
我有被绑定到视图模型与所述特性的的PhoneApplicationPage:
public class MainViewModel : ViewModelBase
{
private string _searchString;
public RelayCommand Search { get; private set; }
public RelayCommand<KeyEventArgs> SearchTextBoxKeyDown { get; private set; }
public string SearchString
{
get { return _searchString; }
set
{
if (_searchString != value)
{
_searchString = value;
RaisePropertyChanged("SearchString");
}
}
}
public MainViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real"
SearchTextBoxKeyDown=new RelayCommand<KeyEventArgs>((e)=>
{
if (e.Key == Key.Enter)
{
// SearchString is empty here?
Search.Execute(null);
}
});
Search = new RelayCommand(() =>
{ // invokes Search with SearchString
},
() =>
{
bool isEnabled = true;
if (string.IsNullOrWhiteSpace(SearchString)) isEnabled = false;
return isEnabled;
});
}
}
观使用MVVM光工具包来援引的KeyDown该方法在自动完成控制(它是一个Telerik的控制,但它看起来像一个TextBox和气味像一个TextBox):
<telerikInput:RadAutoCompleteBox
InputScope="Search"
Text="{Binding SearchString,Mode=TwoWay}"
SuggestionsSource="{Binding MruSearchStrings}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand x:Name="searchStringTextBoxKeyDown" Command="{Binding SearchTextBoxKeyDown, Mode=OneWay}" PassEventArgsToCommand="True">
</cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</telerikInput:RadAutoCompleteBox>
在调用的代码,我只是得到空当我访问SearchString在,这是什么,TextBox是必然 - 即使我已进入文本它。就好像SearchTextBox没有将其数据发送回ViewModel以供ViewModel在事件代码中使用。
我在做什么错?
优秀。非常感谢。多了一点XAML了解。 – 2012-01-16 22:23:47