【问题标题】:DataGridColumn of DataGrid is not updatedDataGrid 的 DataGridColumn 未更新
【发布时间】:2015-11-06 07:37:22
【问题描述】:

我正在开发一个 WPF-MVVM 应用程序。

我有一个空白的 dataGrid,我在其中添加行。 最后一列显示价格。

我想显示总价格作为我添加行的度量

我的代码不起作用。有什么问题?

查看

<DataGrid x:Name="dataGridInvoice" ItemsSource="{Binding Collection}" 
                                   AutoGenerateColumns="False"
                                   SelectedItem="{Binding Selected, Mode=TwoWay}" 
                    <DataGridComboBoxColumn Header="Ref Supplier"
                                        ItemsSource="{Binding DataContext.Reference, Source={StaticResource ProxyElement}}"
                                        DisplayMemberPath="refsup" 
                                        SelectedValueBinding="{Binding refSup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                        SelectedValuePath="refsup"/>
                    <DataGridTextColumn Header="Quantity" Binding="{Binding quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
                    <DataGridTextColumn Header="Price/MOQ" Binding="{Binding unitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
                    <DataGridTextColumn Header="Total Price" Binding="{Binding totalPrice, Mode=TwoWay, StringFormat=N2, UpdateSourceTrigger=PropertyChanged}" Width="*" IsReadOnly="True"/>
                </DataGrid.Columns>
            </DataGrid>

视图模型

public class InvoiceViewModel : ViewModelBase
    {

        public Context ctx = new Context();

        Invoice invoice;

        public InvoiceViewModel()
        {
            Collection = new ObservableCollection<PreInvoice>();
        }

        private ObservableCollection<PreInvoice> collection;

        public ObservableCollection<PreInvoice> Collection
        {
            get
            {
                return collection;
            }
            set
            {
                collection = value;
                OnPropertyChanged("Collection");
                Total = Convert.ToString(Collection.Sum(t => t.totalPrice));

            }
        }

        private string _total;
        public string Total
        {
            get
            {
                return _total;
            }
            set
            {
                _total = value;
                OnPropertyChanged("Total");
            }
        }



        private void Save()
        {


        }

        private void Delete()
        {

        }




        #region "Command"

        private ICommand saveCommand;
        private ICommand removeCommand;


        #endregion

我的模特:

        # region wrapper

        public class PreInvoice : ViewModelBase, IDataErrorInfo
        {

            private string _refSup;

            public string refSup
            {
                get
                {
                    return _refSup;
                }
                set
                {
                    _refSup = value;
                    OnPropertyChanged("refSup");
                }
            }

            private decimal _quantity;

            public decimal quantity
            {
                get
                {
                    return _quantity;
                }
                set
                {
                    _quantity = value;
                    OnPropertyChanged("quantity");
                    totalPrice = _quantity * _unitPrice;
                }
            }

            private decimal _unitPrice;

            public decimal unitPrice
            {
                get
                {
                    return _unitPrice;
                }
                set
                {
                    _unitPrice = value;
                    OnPropertyChanged("unitPrice");
                    totalPrice = _quantity * _unitPrice;
                }
            }

            private decimal _totalPrice;

            public decimal totalPrice
            {
                get
                {
                    return _totalPrice;
                }
                set
                {
                    _totalPrice = value;
                    OnPropertyChanged("totalPrice");

                }
            }
    }

【问题讨论】:

  • “最后一列显示价格”所以显示“价格”是否正确,但未显示其他列?
  • @StepUp,显示所有列。我想说最后一列包含我要计算总和的价格。
  • 好的。显示总价格。但是,你不能总结总价吗?您想在底部有一个新行,其中包含计算的总数量吗?
  • 不只是一个显示总和的文本框

标签: c# wpf datagrid


【解决方案1】:

将 Total 属性定义替换为:

    private string _total;
    public string Total
    {
        get
        {
            _total = Convert.ToString(Collection.Sum(t => t.totalPrice));
            return _total;
        }            
    }

处理您收藏的 CollectionChanged 事件:

    public InvoiceViewModel()
    {
        Collection = new ObservableCollection<PreInvoice>();
        Collection.CollectionChanged += Collection_CollectionChanged;
    }

    void Collection_CollectionChanged(object sender,  System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        OnPropertyChanged("Total");
    }

【讨论】:

  • 只是,我需要双击空白行来更新文本框
  • 所以它会创建一个空行
  • @Cantinou 找不到你?
【解决方案2】:

看起来你在这个prorepty混淆了陈述的顺序:

public ObservableCollection<PreInvoice> Collection
    {
        get
        {
            return collection;
        }
        set
        {
            collection = value;
            Total = Convert.ToString(Collection.Sum(t => t.totalPrice));
            /*or if the above code is not working
            collection=Collection.Sum(t => t.totalPrice));
            */
            OnPropertyChanged("Collection");
        }
    }

OnPropertyChanged("Collection") 方法通过发送事件 PropertyChanged 和您的数据("Collection") 来更新您的 UI。

【讨论】:

  • 我试过了,但它不起作用。集合=Collection.Sum(t => t.totalPrice));无法将十进制转换为“预发票”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 2015-12-04
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
相关资源
最近更新 更多