WPF Entity Framework 获取Datagrid前2个单元格的值然后通过绑定改变第三个单元格的值

WPF Entity Framework get the value of the first 2 cells of the Datagrid then change the value of the third cell through binding

这是我的数据网格的样子,我使用 DataContext 和 CollectionViewSource 来填充网格。

以下是我为此所做的示例代码:

<CollectionViewSource x:Key="BeginningBalanceViewSource" d:DesignSource="{d:DesignInstance {x:Type reportModel:BeginningBalance}, CreateList=True}"/>

<DataGrid  DataContext="{Binding Source={StaticResource BeginningBalanceViewSource}}" ItemsSource="{Binding Source={StaticResource BeginningBalanceViewSource}}">
<DataGrid.Columns>
                <DataGridTemplateColumn Header="Denominations" MinWidth="130">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox BorderThickness="0" IsReadOnly="True" Text="{Binding BBDenomination, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Pieces">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <controls:NumericUpDown BorderThickness="0" Minimum="0" Value="{Binding BBPieces, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Amount" MinWidth="130">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox BorderThickness="0" IsReadOnly="True" Text="{Binding BBAmount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

以下是我如何更改 CollectionViewSource 以更新 DataContext(我从 MSDN 获得):

private CollectionViewSource _beginningBalanceViewSource;
private BeginningBalance _beginningBalance;

_beginningBalanceViewSource = ((CollectionViewSource)(FindResource("BeginningBalanceViewSource")))
_beginningBalance.Source = _entities.BeginningBalances.ToList().Where(x=>x.BBDateManaged.Equals(d)).ToList();

我编辑了实体类型 BeginningBalance.cs(即 ObservableCollection

    public partial class BeginningBalance : INotifyPropertyChanged
    {
        public int BBID { get; set; }
        public double BBDenomination { get; set; }
        private int pieces;
        public int BBPieces { get { return pieces; }
        set
        {
            pieces = value;
            OnPropertyChanged("BBPieces");
            OnPropertyChanged("BBAmount");
        } }
        public double BBAmount { get; set;}

        //public double BBAmount { get{ return BBDenomination * BBPieces; } } 
        //sadly, it returns an error stating that The entity type BeginningBalance is not part of the model for the current context if I ever were to do this.
        public System.DateTime BBDateManaged { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

如果您注意到代码,我添加了 INotifyPropertyChanged 以稍微测试一下,看看金额(DataGrid 的第三列)是否会改变(我知道我在做什么可能是错误的请原谅我愚蠢,因为我缺乏 WPF 经验)

一切正常,我面临的唯一问题是我只需要知道如何获取 Datagrid 的前 2 个单元格的值,然后通过绑定更改第三个单元格的值。第一个 2 个单元格将相互相乘,然后第三个单元格将在我更改第二个单元格的值后自动获得相乘后的值。 NumericUpDownColumn 是唯一非只读的,我尝试添加一个事件,是的,它以某种方式起作用(但它有点搞乱了我的编码方式)。

我还必须为期初余额创建一个 ViewModelClass 吗?如果必须,我不知道如何在单击按钮时保存更改,我的是:

_entities.SaveChanges();

直接存入数据库

我会在解决这个问题后研究 MVVM 模式。

好吧,抱歉我是个白痴,这就像改变我的一样简单

public double BBAmount { get; set;}

public double BBAmount { get{ return BBDenomination * BBPieces; }
            set { }
        }

我只是忘了添加一个 set{} 该死的。