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

时间:2017-05-16 10:32:01

标签: c# wpf data-binding datagrid

我有一个包含总摘要的网格。它们在输入新行时更新。但是,我希望它们在新行或现有行中更改单元格时进行更新。我阅读了一些文章并确定处理单元格更改事件然后在网格上运行UpdateTotalSummary方法可以解决问题,但事实并非如此。下面是我正在使用的代码,我验证了代码是通过调试来命中的。任何帮助表示赞赏:

<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>

这是我的视图模型:

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 { };
}

转换器:

////////////////
    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;
        }
    }

实体:

   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 { };
    }

1 个答案:

答案 0 :(得分:2)

编辑单元格时不会更新ItemsSource。这就是TextBox绑定不起作用的原因。 将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");
    }

将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 { };
}

更新绑定:

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