如何根据第一列值的总和获取列值之一

时间:2014-02-14 08:34:20

标签: c# .net wpf

我有一个Customer类:

        private int _id;
        private int _value;
        private int _dynamicColumn;

        public int ID
        {
            get { return _id; }
            set { _id = value; NotifyPropertyChanged("CustomerId"); }
        }

        public int Value
        {
            get { return _value; }
            set { _value = value; NotifyPropertyChanged("Customername"); }
        }

        public int DynamicValue
        {
            get { return _id + _value; }
            set { _dynamicColumn = value; NotifyPropertyChanged("CustomerAddress"); }
        }

在视图模型中:

ClickCommand = new RelayCommand(paremer =>
                {
                    var customeList = SampleMVVM.Service.Service.GetAllCustomers();
                    Customers = new ObservableCollection<Customer>(customeList);
                    NotifyPropertyChanged("Customers");

                });

现在第三列是Col1 + Col2的总和。但我希望Col3只能归结为Col1的总和值。在这种情况下为897。

尝试在属性获取器上执行此操作但不可行,因为没有所有列的值。

enter image description here

在按钮上单击以下代码:

 var customerRecords = default(IEnumerable<Customer>);

            try
            {
                //string[] names = new string[] { "Steve", "Zones", "Ram", "RaOne", "Chacha", "GOne", "Subhen", "Mithilesh", "Annapurna" };
                Random ranomizer = new Random();


                customerRecords = new List<Customer>(
                    from index in Enumerable.Range(1, 20)
                    select new Customer
                    {
                        ID = ranomizer.Next(35,100),
                        Value = ranomizer.Next(12,89),
                      //  DynamicValue = this.
                    }

            );

我可以在这做什么吗?

修改
我不希望动态值为_id + _值。相反,我希望动态值仅作为第1列值的总和

3 个答案:

答案 0 :(得分:0)

您应该有自定义客户类

public class CustomCustomer
{
    public Customer customers { get; set; }
    public int Sum
    {
      get
      {
        return customers.Select(v => v.ID).Sum();
      }
   }
}

然后,您可以使用Path = Customers.ID,Path = Customers.Value,Path = Sum绑定DataGrid 使用

new ObservableCollection<CustomCustomer>

希望它有所帮助。

答案 1 :(得分:0)

能够使用Element Binding和使用Converter来完成此操作。所以我的XAML现在看起来像:

<DataGrid AutoGenerateColumns="False"
                  Name="dataGrid1"
                  ItemsSource="{Binding Customers}" Grid.Row="1"
                 Grid.ColumnSpan="2" HorizontalAlignment="Stretch">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Binding="{Binding ID}"/>
                <DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
                <DataGridTextColumn Header="DynamicValue" Binding="{Binding ElementName=dataGrid1,Converter={StaticResource columnConverter}}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

有意将Auto Generate设置为False以将转换器添加到第三列。

现在,Converter只查找第一列的行并将其汇总。

 class FirstColumnConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DataGrid dg = value as DataGrid;
            var totalItems = dg.Items.Count;
            int summedValue = 0;

            for (int i = 0; i < totalItems-1; i++)
            {
                    var currentItem = (Customer) dg.Items.GetItemAt(i);
                    summedValue += currentItem.ID;
            }

            return summedValue;   
        }
    }

答案 2 :(得分:-1)

我不太明白你想要完成什么。

如果您想在第三个值中加上第一个和第二个值的总和,则需要一个属性,如:

public DynamicValue
{
    get
    {
        return this.ID + this.Value;
    }
}

如果更改了第一个或第二个值(在属性的设置器中)您还必须调用:

NotifyPropertyChanged("DynamicValue");