Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 7 ViewModel中的RelayCommand:调用无权访问ViewModel中绑定的UI数据_Windows Phone 7_Mvvm Light - Fatal编程技术网

Windows phone 7 ViewModel中的RelayCommand:调用无权访问ViewModel中绑定的UI数据

Windows phone 7 ViewModel中的RelayCommand:调用无权访问ViewModel中绑定的UI数据,windows-phone-7,mvvm-light,Windows Phone 7,Mvvm Light,我正在实现一个基本的搜索文本框->按钮,但我也希望用户能够按键盘/键盘上的回车键 当调用RelayCommand时,我只得到搜索文本框内容的空值 我用的是MVVM灯 我有一个PhoneApplicationPage,它绑定到具有以下属性的ViewModel: public class MainViewModel : ViewModelBase { private string _searchString; public RelayCommand Search { get; private

我正在实现一个基本的搜索文本框->按钮,但我也希望用户能够按键盘/键盘上的回车键

当调用RelayCommand时,我只得到搜索文本框内容的空值

我用的是MVVM灯

我有一个PhoneApplicationPage,它绑定到具有以下属性的ViewModel:

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;
        });
    }
}
public类MainViewModel:ViewModelBase
{
私有字符串_searchString;
公共中继命令搜索{get;private set;}
public RelayCommand SearchTextBoxKeyDown{get;private set;}
公共字符串搜索字符串
{
获取{return\u searchString;}
设置
{
if(_searchString!=值)
{
_searchString=值;
RaisePropertyChanged(“搜索字符串”);
}
}
}
公共主视图模型()
{
如果(IsInDesignMode)
{
//代码在混合-->创建设计时数据中运行。
}
其他的
{
//代码“真实地”运行
SearchTextBoxKeyDown=新的RelayCommand((e)=>
{
如果(e.Key==Key.Enter)
{
//搜索字符串在这里是空的吗?
Search.Execute(null);
}
});
搜索=新的RelayCommand(()=>
{//使用SearchString调用搜索
},
() =>
{
bool isEnabled=true;
if(string.IsNullOrWhiteSpace(SearchString))isEnabled=false;
返回被禁止;
});
}
}
该视图使用MVVM Light toolkit在AutoComplete控件(它是Telerik控件,但看起来像文本框,闻起来像文本框)上调用KeyDown方法:


在调用的代码中,当我访问SearchString时,我只得到null,这是TextBox绑定到的内容,即使我已经在其中输入了文本。这几乎就像SearchTextBox没有将其数据发送回ViewModel,以便ViewModel在事件代码中拾取一样


我做错了什么?

您的ViewModel没有使用TextBox中的值进行更新,因为当您按Enter键时,您仍然在TextBox中拥有焦点。默认情况下,ViewModel将在您离开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>