//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属性在添加多个值时返回零。
答案 0 :(得分:1)
您显示的Total
属于您首先创建的单个D对象,但与您的DerivedClasses
列表没有任何关联(我必须说明,您是&#39;}重新包装作为一个单独的类)。仅仅因为有一个基类,它并不意味着它的属性将存储价值全球&#39;所以任何实例都可以读取它们。 static
属性将作为一个属性,但在您描述它的场景中,在类之外指定一个表示总和的新属性/变量会更有意义。此外,在setter中添加数字并不能很好地工作,因为它会注册任何值更改,并且您可能最终会多次添加相同的属性(除非这是您的&#39;重新尝试......)。
编辑:
对于初学者,我会创建一个ViewModel类,您的页面DataContext
将绑定到该类:
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
事件。如果触发偶数的属性是X或Y,则它将触发另一个事件以重新计算总和(ItemPropertyChanged
)。
答案 1 :(得分: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给你的警告,它可能告诉你这个&#34;方法覆盖非虚基类方法,使基类方法虚拟或使用关键字new&#34;
另外我认为你的总计算很乱,你总是加上总数而不只是做X + Y.