0
//In Test.xaml 

 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
        </Grid.RowDefinitions>
        <DataGrid  Grid.Row="0"  AutoGenerateColumns="False" ItemsSource="{Binding}"   Name="dtGridTran" HorizontalAlignment="Left"   CanUserAddRows="True"   >
            <DataGrid.Columns>
                <DataGridTextColumn Header="X" Binding="{Binding Path=X, UpdateSourceTrigger=PropertyChanged}"   Width="200"/>               
                <DataGridTextColumn Header="Y" Binding="{Binding Path=Y, UpdateSourceTrigger=PropertyChanged}" Width="200"  />
            </DataGrid.Columns>
        </DataGrid>
        <Label Grid.Row="1" Content=" Total Sum of x and Y">

        </Label>
        <TextBox  Grid.Row="1" Text="{Binding Path=Total, UpdateSourceTrigger=PropertyChanged}" Margin="150,0,0,0" Width="100"></TextBox>       
    </Grid>

//In Test.xaml.cs file
public partial class Test : Page
    {

        Derivedclass D = new Derivedclass();
        public Test()
        {
            InitializeComponent();
            this.DataContext = D;
            dtGridTran.ItemsSource = new TestGridItems();
        }

    }

public class TestGridItems : List<Derivedclass>
{
}

// In Base class
public class Baseclass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private int mTotal = 0;
        private int mID = 0;

        public int Total
        {
            get { return mTotal; }
            set
            {
                mTotal = value; OnPropertyChanged("Total");
            }
        }
        public int ID
        {
            get { return mID; }
            set { mID = value; }
        }
        public string Item = "xxx";
        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

    }

In Derivedclass.cs

    public class Derivedclass : Baseclass, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private int mX = 0;
        private int mY = 0;
        public int X
        {
            get { return mX; }
            set
            {
                mX = value;
                base.Total = base.Total + mX;
                                OnPropertyChanged("Total");

            }
        }
        public int Y
        {
            get { return mY; }
            set
            {
                mY = value;
                base.Total = base.Total + mY;
                OnPropertyChanged("Total");
            }
        }
         // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }
}

Here am trying to find total sum of x and y value. but m getting total sum zero . total property is in base class. i want total sum of x and y column that i want to display in a textbox.. but Total property of baseclass return zero while adding multiple value.

shubh
  • 61
  • 1
  • 3
  • 11

2 Answers2

1

The Total you're displaying belongs to a single D object which you create first but there is no connection to your List of DerivedClasses (which, I must say, you're wrapping weirdly as a separate class). Just because there is a base class it doesn't mean its properties will store values 'globally' so any instance can read them. A static property would act as one but in the scenario you're describing it would make more sense to designate a new property/variable outside your classes which would represent a sum. Besides, adding up numbers in a setter isn't going to work well as it will register any value changes and you might end up adding same property multiple times (unless this is what you're trying to achieve...).

Edit:

For starters, I would create a ViewModel class which your page's DataContext would bind to:

public class MyViewModel : INotifyPropertyChanged
{
    // backing fields, etc...

    public List<DerivedClass> Items
    {
        get { return items; }
        set
        {
            items = value;
            OnPropertyChanged("Items");
        }
    }

    public int Sum
    {
        get
        {
            return this.Items != null ? this.Items.Sum(i => i.X + i.Y) : 0;
        }
    }

   ...       

   public void PopulateItems()
   {
       this.Items = MyMethodToGetItems();
       foreach (var item in this.Items)
       {
           item.PropertyChanged += this.ItemPropertyChanged;
       }
   }

    private void ItemPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
    {
        if (propertyChangedEventArgs.PropertyName == "X" || propertyChangedEventArgs.PropertyName == "Y")
        {
            OnPropertyChanged("Sum");
        }
    }
}

In the PopulateItems method it will subscribe to the PropertyChaned event of each item in the collection. If the property which triggered the even is either X or Y it will then fire another event to recalculate the sum (ItemPropertyChanged).

Jerrington
  • 113
  • 4
0

I believe the problem you are having is because of the separate implementations of INotifyPropertyChanged, removed the implementation from the DerivedClass and it should be ok, the binding is automatically hooking to the classes PropertyChanged event but because this is a different event to the base class it is not catching any base class PropertyChanged events being fired.

So the DerivedClass should just look like this

public class Derivedclass : Baseclass
{
    private int mX = 0;
    private int mY = 0;
    public int X
    {
        get { return mX; }
        set
        {
            mX = value;
            base.Total = base.Total + mX;
            OnPropertyChanged("Total");

        }
    }
    public int Y
    {
        get { return mY; }
        set
        {
            mY = value;
            base.Total = base.Total + mY;
            OnPropertyChanged("Total");
        }
    }
}

If you look at the warnings visual studio is giving you it has probably told you about this "method overrides non-virtual base class method, make base class method virtual or use keyword new"

Also I think your total calculation is messed up, you are always adding on to the total rather than just doing X + Y.

ndonohoe
  • 9,320
  • 2
  • 18
  • 25
  • I think he's trying to display the sum of all his elements in the grid so that will never work regardless of having X+Y or base.Total + mX. – Jerrington Oct 28 '14 at 13:53
  • Ah ok, I see now. Then he definitely needs to populate a list as you say. – ndonohoe Oct 28 '14 at 14:27