简体   繁体   English

更改WPF DataGrid单元格时如何更新总计摘要?

[英]How to Update Total summary when WPF DataGrid cells are changed?

I have a grid with total summaries. 我有一个汇总表。 They update when a new row is entered. 当输入新行时,它们将更新。 However, I want them to update as cells are changed in a new or existing row. 但是,我希望它们随着新行或现有行中单元格的更改而更新。 I read a few articles and determined that handling the cell changed event and then running the UpdateTotalSummary method on the grid would do the trick, but it does not. 我阅读了几篇文章,并确定处理单元更改事件,然后在网格上运行UpdateTotalSummary方法可以解决问题,但事实并非如此。 Below is the code I am using and I verified that the code is being hit via debugging. 以下是我正在使用的代码,并且我验证了通过调试找到的代码。 Any help is appreciated: 任何帮助表示赞赏:

<Grid DataContext="{Binding Source={StaticResource VM}}">
    <Grid.Resources>
        <local:TotalSumConverter x:Key="sumConverter" />
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <DataGrid x:Name="myGrid" AutoGenerateColumns="False" ItemsSource="{Binding TrsFundDocItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Amount,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="Amount"/>
            <DataGridTextColumn Binding="{Binding FCAmount,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="FCAmount"/>
        </DataGrid.Columns>
    </DataGrid>
    <Grid Grid.RowSpan="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="1" Grid.Column="1" Margin="8">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition  Width="auto"/>
                <ColumnDefinition  Width="auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="SumTotal :" Grid.Row="0" Grid.Column="0"/>


            <TextBox Grid.Row="0" Grid.Column="1"  Text="{Binding ItemsSource, ConverterParameter=Amount, Converter={StaticResource sumConverter}, ElementName=myGrid, FallbackValue=0, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        </Grid>
    </Grid>
    </Grid>

This is my View Model : 这是我的视图模型:

public class WindowxViewModel : INotifyPropertyChanged
{
    public WindowxViewModel()
    {
        TrsFundDocItems = new ObservableCollection<TrsFundDocItem> { new TrsFundDocItem { Amount = 10, FCAmount = 1 }, new TrsFundDocItem { Amount = 5, FCAmount = 8 } };
    }


    private ObservableCollection<TrsFundDocItem> _TrsFundDocItems;

    public ObservableCollection<TrsFundDocItem> TrsFundDocItems
    {
        get { return _TrsFundDocItems; }
        set
        {
            if (_TrsFundDocItems == value) return;
            _TrsFundDocItems = value;
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(TrsFundDocItems)));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

Converter : 转换器:

////////////////
    public class TotalSumConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var users = value as IEnumerable<object>;
            if (users == null)
                return "$0.00";

            double sum = 0;

            foreach (var u in users)
            {
                sum += ((TrsFundDocItem)u).Amount;
            }


            return sum.ToString("c");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return 0;
        }
    }

Entity : 实体 :

   public class TrsFundDocItem:INotifyPropertyChanged
    {
        //public double Amount { get; set; }
        private double amount;

        public double Amount
        {
            get { return amount; }
            set {
                if (amount == value) return;
                amount = value;
                PropertyChanged(this, new PropertyChangedEventArgs(nameof(Amount)));
            }
        }

        public double FCAmount { get; set; }

        public event PropertyChangedEventHandler PropertyChanged=delegate { };
    }

ItemsSource is not updated when you edit the cell. 当您编辑单元格时,ItemsSource不会更新。 That is why TextBox binding does not work. 这就是为什么TextBox绑定不起作用的原因。 Add TotalAmount property and Update method to the WindowxViewModel : 将TotalAmount属性和Update方法添加到WindowxViewModel

    public string TotalAmount
    {
        get { return _totalAmount; }
        set
        {
            if (_totalAmount == value) return;
            _totalAmount = value;
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(TotalAmount)));
        }
    }

    private void UpdateAmount()
    {
        double sum = 0;

        foreach (var u in TrsFundDocItems)
        {
            sum += u.Amount;
        }


        TotalAmount = sum.ToString("c");
    }

Pass UpdateAmount method to TrsFundDocItem: 将UpdateAmount方法传递给TrsFundDocItem:

public class TrsFundDocItem : INotifyPropertyChanged
{
    private readonly Action _updateAction;

    //public double Amount { get; set; }
    private double amount;

    public TrsFundDocItem(Action updateAction)
    {
        _updateAction = updateAction;
    }
    public double Amount
    {
        get { return amount; }
        set
        {
            if (amount == value) return;
            amount = value;
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(Amount)));
            _updateAction();
        }
    }

    public double FCAmount { get; set; }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

Update the binding: 更新绑定:

<TextBox Grid.Row="0" Grid.Column="1"  Text="{Binding TotalAmount, FallbackValue=0, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM