2014-10-28 48 views
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)); 
      } 
     } 
    } 
} 

这里我试图找到x和y值的总和。但总得分为零。总财产在基础类。我想要在文本框中显示x和y列的总和..但基本类的Total属性在添加多个值时返回零。无法将继承的类绑定到数据网格wpf c#

+0

为什么要设置base.Total,而不是总?我认为你设置的值只有在实例被转换为'BaseClass'时才会显现。 – 2014-10-28 10:52:25

+0

总属性是在BaseClass ..我想设置一个值从派生类的基类属性 – shubh 2014-10-28 10:54:54

+0

你在哪里设置派生类对象的属性?在做这个之前.DataContext = D; – bit 2014-10-28 10:56:36

回答

1

您正在显示的Total属于您首先创建的单个D对象,但与您的DerivedClasses列表(我必须说,您正在将其怪异地包装为单独的类)没有关系。仅仅因为存在一个基类,并不意味着它的属性会将值存储为“全局”,因此任何实例都可以读取它们。一个static属性将作为一个,但在你描述的情况下,在你的类之外指定一个新的属性/变量将会更有意义,这将代表一笔款项。此外,在setter中添加数字并不会很好,因为它会注册任何值更改,并且您最终可能会多次添加相同的属性(除非这是您尝试实现的目标...)。

编辑:

对于初学者来说,我会创造出你的页面的DataContext将绑定到ViewModel类:

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"); 
     } 
    } 
} 

PopulateItems方法将订阅在该PropertyChaned事件的每个项目的采集。如果触发even的属性是X或Y,它将触发另一个事件重新计算总和(ItemPropertyChanged)。

+0

好吧然后如何找到总和? – shubh 2014-10-28 11:38:44

+0

@shubh,看我的编辑。 – Jerrington 2014-10-28 12:12:36

0

我相信你遇到的问题是因为INotifyPropertyChanged的单独实现,从DerivedClass中删除了实现,它应该没问题,绑定会自动挂接到类PropertyChanged事件,但因为这是一个不同的事件基类没有捕获正在触发的任何基类PropertyChanged事件。

所以DerivedClass应该只是看起来像这样

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"); 
     } 
    } 
} 

如果你看一下警告Visual Studio是给你那么它可能已经告诉你这个“方法会覆盖非虚基类的方法,使基类方法虚拟或使用new关键字”

此外,我认为你的总计算搞砸了,你总是添加到总量,而不是只是在做X + Y

+0

我想他试图在网格中显示他所有元素的总和,这样无论X + Y还是base.Total + mX都不会工作。 – Jerrington 2014-10-28 13:53:31

+0

好吧,我明白了。那么他肯定需要按照你的说法填充一个列表。 – ndonohoe 2014-10-28 14:27:53