WPF上的WPF DataGrid源更新已更改 [英] WPF DataGrid source updating on cell changed

查看:170
本文介绍了WPF上的WPF DataGrid源更新已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的新手,我用它来构建一个销售点系统。

I am new to the WPF ,and i use it to build a point of sale system.

我有一个 DataGrid 控件在主窗口中绑定到 ObservableCollection 项目,收银员将进入/扫描项目要出售的每个项目的默认数量为1,但可用于出纳手动更改数量。

I have a DataGrid control in the main window bound to an ObservableCollection of Item, the cashier will enter/scan the items to be sold the default quantity for each item is 1 but it is available for the cashier to change the quantity manually.

每当我更改数量,它应该更新总计当我将单元格移动到行上的另一个单元格时,价格与项目价格的总和,但不会发生,仅当我进入另一行而不是同一行中的另一个单元格时才更新源。

Whenever I change the quantity, it should update the total price with the sum of the items' prices when I leave the cell to another cell on the row, but it doesn't happen, the source is updated only when I go to another row not another cell in the same row.

有没有办法强制 DataGrid 在单元格更改而不是行更新源?

Is there anyway to force the DataGrid to update the source when the cell is changed rather than the row?

推荐答案

是的,这是可能的。您的问题与 DataGrid - 更改编辑行为基本相同

Yes, this is possible. Your question is basically the same as DataGrid - change edit behaviour

下面的代码主要来自Quartermeister的答案,但我添加了一个 DependencyProperty BoundCellLevel,您可以在需要 DataGrid 绑定在当前单元格更改时被更新。

The code below is mostly from Quartermeister's answer but I added a DependencyProperty BoundCellLevel that you can set when you need a DataGrid binding to be updated when the current cell changes.

public class DataGridEx : DataGrid
{
    public DataGridEx()
    {

    }

    public bool BoundCellLevel
    {
        get { return (bool)GetValue(BoundCellLevelProperty); }
        set { SetValue(BoundCellLevelProperty, value); }
    }

    public static readonly DependencyProperty BoundCellLevelProperty =
        DependencyProperty.Register("BoundCellLevel", typeof(bool), typeof(DataGridEx), new UIPropertyMetadata(false));

    protected override Size MeasureOverride(Size availableSize)
    {
        var desiredSize = base.MeasureOverride(availableSize);
        if ( BoundCellLevel )
            ClearBindingGroup();
        return desiredSize;
    }

    private void ClearBindingGroup()
    {
        // Clear ItemBindingGroup so it isn't applied to new rows
        ItemBindingGroup = null;
        // Clear BindingGroup on already created rows
        foreach (var item in Items)
        {
            var row = ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;
            row.BindingGroup = null;
        }
    }
}

这篇关于WPF上的WPF DataGrid源更新已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆