无法将继承的类绑定到数据网格 wpf c#

本文关键字:数据网 数据 网格 wpf 绑定 继承 | 更新日期: 2023-09-27 18:28:09

//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值的总和。 但是 m 得到总和零 . 总属性位于基类中。 我想要要在文本框中显示的 x 和 y 列的总和..... 但是基类的 Total 属性在添加多个值时返回零。

无法将继承的类绑定到数据网格 wpf c#

您显示的Total属于您首先创建的单个 D 对象,但与您的DerivedClasses列表没有联系(我必须说,您奇怪地将其包装为一个单独的类(。仅仅因为有一个基类,并不意味着它的属性将"全局"存储值,以便任何实例都可以读取它们。static属性将充当一个属性,但在您描述的场景中,在类之外指定一个新属性/变量来表示总和会更有意义。此外,在二传手中将数字相加不会很好地工作,因为它会记录任何值更改,并且您最终可能会多次添加相同的属性(除非这是您要实现的......

编辑:

首先,我将创建一个 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 (。

我相信

您遇到的问题是因为 INotifyPropertyChanged 的单独实现,从派生类中删除了实现,它应该没问题,绑定会自动挂钩到类 PropertyChanged 事件,但由于这是与基类不同的事件,它没有捕获任何正在触发的基类 PropertyChanged 事件。

所以派生类应该看起来像这样

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。