使用WPF的DataGrid将值从单元格绑定到单元格 [英] Binding value from cell to cell using DataGrid of WPF

查看:104
本文介绍了使用WPF的DataGrid将值从单元格绑定到单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用WPF的DataGrid将值从单元格绑定到单元格时出现问题。让我先解释一下我的期望。我想创建一个像这样的DataGrid:



数量|价格|支付

(用户输入)| (用户输入)| (自动计算并显示支付=数量*价格)

代码隐藏正常工作。但GUI未更新。你能支持我解决这个问题吗?



这是我的XAML:



I have an issue when binding value dynamic from cell to cell using DataGrid of WPF. Let me explain my expectation first. I want to create a DataGrid like that:

Quantity | Prices | Pay
(user input) | (user input ) | (auto calculate and display Pay = Quantity * Prices )
the code behide work correctly. But the GUI is not updated. Could you please support me to resolve this issue?

Here is my XAML:

<grid>
    <datagrid x:name="DGTest" itemssource="{Binding m_Products}" horizontalalignment="Left" margin="40,80,0,0" verticalalignment="Top" height="134" width="414" xmlns:x="#unknown">
              AutoGenerateColumns="False">
        <datagrid.columns>
            <datagridtextcolumn binding="{Binding Quantity, UpdateSourceTrigger=LostFocus}" header="Quantity" width="60" />
            <datagridtextcolumn binding="{Binding Prices, UpdateSourceTrigger=LostFocus}" header="Prices" width="60" />
            <datagridtextcolumn binding="{Binding Pay, UpdateSourceTrigger=LostFocus}" header="Pay" width="60" isreadonly="True" />
        </datagrid.columns>
    </datagrid>

</grid>



这是我的代码behide:




Here is my code behide:

MainWindow : Window
{
    ObservableCollection<product> m_Products = new ObservableCollection<product>();
    private class PRODUCT : INotifyPropertyChanged
    {
        int _quantity;
        public int Quantity
        {
            get { return _quantity;  }
            set
            {
                if (_quantity != value)
                {
                    _quantity = value;
                    _pay = _quantity * _prices;
                }
            }
        }
        int _prices;
        public int Prices
        {
            get{ return _prices;  }
            set
            {
                if (_prices != value)
                {
                    _prices = value;
                    _pay = _quantity * _prices;
                }
            }
        }
        int _pay;
        public int Pay
        {
            get { return _pay; }
            set
            {
                if (_pay != value)
                {
                    _prices = value;
                    _pay = _quantity * _prices;
                }
            }
        }
        void OnPropertyChanged(string prop)
        {               
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    public MainWindow()
    {

        InitializeComponent();

        m_Products.Add(new PRODUCT { Quantity = 10, Prices = 20, Pay = 200 });
        m_Products.Add(new PRODUCT { Quantity = 2, Prices = 20, Pay = 40 });

        DGTest.ItemsSource = m_Products;

        DataContext = this;
    }
}

推荐答案

我建​​议你用以下内容替换你的产品类:



I would suggest you replace your product class with the following:

private class PRODUCT : INotifyPropertyChanged
    {
        int _quantity;
        public int Quantity
        {
            get { return _quantity;  }
            set
            {
                if (_quantity != value)
                {
                    _quantity = value;
                    OnPropertyChanged("Quantity");
                    OnPropertyChanged("Pay");
                }
            }
        }
        int _prices;
        public int Prices
        {
            get{ return _prices;  }
            set
            {
                if (_prices != value)
                {
                    _prices = value;
                    OnPropertyChanged("Prices");
                    OnPropertyChanged("Pay");
                }
            }
        }
        int _pay;
        public int Pay
        {
            get { return _quantity * _prices; }
                        
        }
        void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }





我也会通过删除更新属性来更改此绑定,如下所示:



I would also change this binding by removing the update attribute as below:

<datagridtextcolumn binding="{Binding Pay}" header="Pay" width="60" isreadonly="True" />





至于解释,您没有告诉您的GUI更新。必须使用已更新的属性名称调用OnPropertyChanged事件。当你有很多计算属性时,这可能会很麻烦,尽管有建议的解决方案允许你部分自动化这个过程。



As for an explanation, at no point were you telling your GUI to update. To have to call your "OnPropertyChanged" event with the name of the properties that have been updated. When you have a lot of calculated properties this can be a pain, although there are advised solutions around that allow you to partially automate that process.


这篇关于使用WPF的DataGrid将值从单元格绑定到单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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