【问题标题】:Binding ICommand WPF ObservableCollection DataGrid绑定 ICommand WPF ObservableCollection DataGrid
【发布时间】:2015-04-02 02:24:09
【问题描述】:

我正在尝试让 WPF MVVM 模板与我在 WPF 但非 MVVM 应用程序中执行的基本功能一起工作。在这种情况下,我试图捕获 RowEditEnding 事件(我就是)来验证已更改行上的数据(这就是问题所在)。

在 XAML 中我使用了事件触发器:

        <DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"
                      ItemsSource="{Binding oDoc.View}">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="docIDColumn" Binding="{Binding DocId}" Header="ID" Width="65"/>
            <DataGridTextColumn x:Name="DocumentNumberColumn" Binding="{Binding Number}" Header="Document Number" Width="*"/>
            <DataGridTextColumn x:Name="altIDColumn" Binding="{Binding AltID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Alt" Width="55"/>
        </DataGrid.Columns>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="RowEditEnding">
                <i:InvokeCommandAction Command="{Binding DocRowEdit}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>

使用委托命令路由到处理程序:

        public ObservableCollection<Document> oDoc
    {
        get
        {
            return _oDoc;
        }
    }

    public ICommand DocRowEdit
    {
        get { return new DelegateCommand(DocumentRowEditEvent); }
    }

    public void DocumentRowEditEvent()
    {
        //How do I find the changed item?
        int i = 1;
    }

我还没有找到方法来查找具有待处理更改的 ObservableCollection (oDoc) 成员。我注意到数据网格正在进行一些验证,如果我输入非数值,我想要更改的 AltID 字段将突出显示红色。但我想自己处理验证和相关的消息传递。我错过了什么?我正在考虑以某种方式引发属性更改事件,但没有找到如何将这样的东西连接到:

        protected void RaisePropertyChangedEvent(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

最后两个代码块来自我的 ViewModel 类,除了在 MainWindow 构造函数中实例化 ViewModel 之外,我试图在没有任何代码的情况下执行此操作。

【问题讨论】:

    标签: c# wpf mvvm datagrid


    【解决方案1】:

    您可以向 ViewModel 添加属性:

    public Document CurrentDocument
    {
        get
        {
            return _currentDocument;
        }
        set
        {
            if (value != _currentDocument)
            {
                _currentDocument = value;
                OnPropertyChanged("CurrentDocument") // If you implement INotifyPropertyChanged
            }
        }
    }
    

    然后您可以将其绑定到 DataGrid 的 SelectedItem 属性:

    <DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"
                          ItemsSource="{Binding oDoc.View}" SelectedItem="{Binding CurrentDocument}">
    

    因此您的编辑方法将是:

    public void DocumentRowEditEvent()
    {
        CurrentDocument.Number = DateTime.Now.Ticks;
        /* And so on... */
    }
    

    希望对你有帮助。

    【讨论】:

    • 好的,太好了。这个绑定非常强大。相对于 OnPropertyChanged,我也实现了这一点,但我不确定我是否完全掌握了它的作用。似乎它只是触发一个事件,如果我想处理该事件,我需要向数据网格中的 XAML 块添加另一个事件触发器。那正确吗?还是有另一种方法可以在处理程序中捕获它?我在调试器中使用了它,看到我的 OnPropertyChanged 函数被调用,它调用了 handler(this, new PropertyChangedEventArgs(propertyName));,但我不知道如何使用它。
    • 我同意,它是一个强大的工具!关于OnPropertyChanged 事件(即INotifyPropertyChanged 接口),它不是触发事件的简单机制。实际上,它允许您的 UI 了解代码执行的数据更改。您可以在hereCodeProject 上阅读更多内容。
    猜你喜欢
    • 2015-01-26
    • 2013-10-14
    • 2014-08-23
    • 2017-04-16
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    相关资源
    最近更新 更多