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

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

问题描述

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

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

我在主窗口有一个DataGrid控件绑定到一个ItemObservableCollection,收银员会输入/扫描需要的物品售出每件商品的默认数量为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 DataGrid 源更新已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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