首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定DataGrid.CurrentItem

绑定DataGrid.CurrentItem
EN

Stack Overflow用户
提问于 2013-01-17 08:32:11
回答 1查看 1.1K关注 0票数 0

全,

我有一个DataGrid,它的CurrentItem属性被绑定到我的VM中的某个属性。我还有一个ICommand,它创建一个新对象,将其添加到数据网格的ItemSource绑定到的集合中,并将CurrentItem设置为等于新对象。

一切都运行得很好,除了由于某种原因,在通过代码更改CurrentItem绑定时没有调用提交。

请参阅下面的相关代码部分。

XAML:

代码语言:javascript
复制
<DataGrid ItemSource={Binding} CurrentItem={Binding Path=CurrentItem, UpdateSourceTrigger=PropertyChange, Source={StaticResource VM}}>
    <DataGrid.InputBindings>
        <KeyBinding Command={Binding Path=AddNewItemCommand, UpdateSourceTrigger=PropertyChanged, Source={StaticResource VM}} Key="OemPlus" Modifiers="Control" />
    </DataGrid.InputBindings>
    <DataGrid.Columns>
        ...
    </DataGrid.Columns>
</DataGrid>

虚拟机:

代码语言:javascript
复制
Class cVM:INotifyPropertyChanged
{

    /*...RaisePropertyChanged(string str) method implimented here to handle PropertyChanged event*/

    private ICommand _AddNewItemCommand; //defined in Constructor.  Adds new item to Collection and sets CurrentItem property.     
    ICommand AddNewItemCommand{ get { return _AddNewItemCommand; } }

    private object _CurrentItem;
    public object CurrentItem
    {
        get
        {
            return _CurrentItem;
        }
        set
        {
            _CurrentItem = value;
            RaisePropertyChanged("CurrentItem");
        }
    }
    /*...*/
}
EN

回答 1

Stack Overflow用户

发布于 2013-01-18 03:54:32

不是一个纯粹的MVVM解决方案,但以下是我为解决问题所做的工作:

代码语言:javascript
复制
<DataGrid ItemSource={Binding} CurrentItem={Binding Path=CurrentItem, UpdateSourceTrigger=PropertyChange, Source={StaticResource VM}}>
    <DataGrid.InputBindings>
        <KeyBinding Command={Binding Path=AddNewItemCommand, UpdateSourceTrigger=PropertyChanged, Source={StaticResource VM}} CommandParamater={Binding RelativeSource={RelativeSource AncestorType=DataGrid}}" Key="OemPlus" Modifiers="Control" />
    </DataGrid.InputBindings>
    <DataGrid.Columns>
        ...
    </DataGrid.Columns>
</DataGrid>

然后在我的ICommand (它是一个类型安全的RelayCommand,参见底部的代码)中,我执行:

代码语言:javascript
复制
param => { param.SetCurrentValue(DataGrid.CurrentItemProperty, null); param.Dispatcher.DoEvents() /*An extension that pushes a dispather frame with dispatherpriority.background, which forces events to fire that might be in an asynchronious call-stack */; Object obj = new Object(); /*Whatever your Object is, of course */ MyCollection.Add(obj); CurrentItem = obj; }

无论如何,我希望这对下一个人有帮助。我不知道在这种情况下是否需要DoEvents扩展,但为了安全起见,我使用了它。此外,对于param,我的代码知道它是DataGrid,因为我在构造函数中包含了一个接受泛型的RelayCommand版本

(如果感兴趣,请键入safe relaycommand。在互联网上的某个地方找到,所以不能完全归功于)

代码语言:javascript
复制
public class RelayCommand<T> : ICommand
{
    private Action<T> _execute;
    private Predicate<T> _canexecute;

    public RelayCommand(Action<T> execute, Predicate<T> canexecute)
    {
        _execute = execute;
        _canexecute = canexecute;
    }

    public bool CanExecute(object parameter)
    {
        if (_canexecute == null) return false;
        else return _canexecute((T)parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute((T)parameter);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14370352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档