简体   繁体   English

将子数据汇总到父级进行绑定

[英]Aggregate data from child into parent for Binding

This post is completely edited to provide a more solid example and simplified ask 此帖子已完全编辑,以提供更加可靠的示例和简化的问题

I'm looking to have the results from Child changes be reflected in the Parent, particularly the field NumberOfChildrenWithDegrees . 我希望将Child更改的结果反映在Parent中,尤其是NumberOfChildrenWithDegrees字段。 If you check both boxes with bindings of HasUniversityDegree and HasHighSchoolDegree then HasTwoDegrees in the Child View updates. 如果您选中具有HasUniversityDegreeHasHighSchoolDegree绑定的两个框,则子视图中的HasTwoDegrees更新。 What I would like is the number of Children with HasTwoDegrees be reflected in the Parent VM. 我想要的是HasTwoDegrees的子女HasTwoDegrees会反映在父虚拟机中。

I thought this would just be simple, but it turns out that it doesn't work - checking two boxes changes nothing in the Parent View (the <TextBlock Text="{Binding NumberOfChildrenWithDegrees,Mode=TwoWay,UpdateSourceTrigger=Explicit}"/> area. How can I get this number change to happen real time? 我认为这只是简单,但事实证明它不起作用 - 检查两个框在父视图中没有任何改变( <TextBlock Text="{Binding NumberOfChildrenWithDegrees,Mode=TwoWay,UpdateSourceTrigger=Explicit}"/> 我怎样才能让这个数字变化实时发生?

I've created a small sample for Windows Phone that can be plugged directly into a page called TestPage . 我已经为Windows Phone创建了一个小样本,可以直接插入名为TestPage的页面。 Just place the XAML in the content grid and the code in the code-behind (VB). 只需将XAML放在内容网格中,然后将代码放在代码隐藏(VB)中即可。

XAML: XAML:

    <Grid x:Name="ContentPanel" Background="{StaticResource PhoneChromeBrush}" Grid.Row="1">
        <ListBox x:Name="parentLB" Margin="12,12,12,12" HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" Margin="0,12,0,0">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Parent Name: "/>
                            <TextBlock Text="{Binding Name}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Number of Children with Degrees: "/>
                            <TextBlock Text="{Binding NumberOfChildrenWithDegrees,Mode=TwoWay,UpdateSourceTrigger=Explicit}"/>
                        </StackPanel>
                        <ListBox Margin="12,0,0,0" x:Name="group2" ItemsSource="{Binding Children,Mode=TwoWay}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="Child Name: "/>
                                            <TextBlock Text="{Binding Name}"/>
                                        </StackPanel>
                                            <CheckBox Content="Has High School Degree:" IsChecked="{Binding HasHighSchoolDegree,Mode=TwoWay}"/>
                                            <CheckBox Content="Has University Degree: " IsChecked="{Binding HasUniversityDegree,Mode=TwoWay}"/>
                                            <CheckBox Content="Has Two Degrees? " IsEnabled="False" IsChecked="{Binding HasTwoDegrees}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

Code-behind: 代码隐藏:

Imports System.Collections.ObjectModel
Imports System.ComponentModel

Partial Public Class TestPage
    Inherits PhoneApplicationPage

    Public Sub New()
        InitializeComponent()
        parentLB.ItemsSource = Grandparent1.ParentGroups
    End Sub
    Public Function Grandparent1() As GrandParent
        Dim ParentGroups As New List(Of Parent)
        ParentGroups.Add(Parent1)
        ParentGroups.Add(Parent2)
        Dim gp As New GrandParent
        gp.ParentGroups = ParentGroups
        Return gp
    End Function
    Public Function Parent2() As Parent
        Dim p As New Parent With {.Name = "Tom"}
        Dim c1 As New Child With {.Name = "Tammy"}
        Dim c2 As New Child With {.Name = "Timmy"}
        Dim children As New List(Of Child)
        children.Add(c1)
        children.Add(c2)
        p.Children = children
        Return p
    End Function

    Public Function Parent1() As Parent
        Dim p As New Parent With {.Name = "Carol"}
        Dim c1 As New Child With {.Name = "Carl"}
        c1.HasHighSchoolDegree = True
        c1.HasUniversityDegree = True
        Dim c2 As New Child With {.Name = "Karla"}
        Dim children As New List(Of Child)
        children.Add(c1)
        children.Add(c2)
        p.Children = children
        Return p
    End Function
End Class

Public Class GrandParent
    Inherits BindableBase
    Public Property ParentGroups As List(Of Parent)
End Class
Public Class Parent
    Inherits BindableBase
    Public Property Name As String

    Private _children As List(Of Child)
    Public Property Children As List(Of Child)
        Get
            Return Me._children
        End Get
        Set(value As List(Of Child))
            Me.SetProperty(Me._children, value)
        End Set
    End Property

    Private _numberOfChildrenWithDegrees As Integer
    Public Property NumberOfChildrenWithDegrees As Integer
        Get
            Return Children.Where(Function(f) f.HasTwoDegrees).Count
        End Get
        Set(value As Integer)
            Me.SetProperty(Me._numberOfChildrenWithDegrees, value)
        End Set
    End Property
End Class
Public Class Child
    Inherits BindableBase
    Public Property Name As String

    Public ReadOnly Property HasTwoDegrees As Boolean
        Get
            Return HasHighSchoolDegree AndAlso HasUniversityDegree
        End Get
    End Property

    Private _hasUniversityDegree As Boolean
    Public Property HasUniversityDegree As Boolean
        Get
            Return Me._hasUniversityDegree
        End Get
        Set(value As Boolean)
            Me.SetProperty(Me._hasUniversityDegree, value)
            OnPropertyChanged("HasTwoDegrees")
        End Set
    End Property

    Private _hasHighSchoolDegree As Boolean
    Public Property HasHighSchoolDegree As Boolean
        Get
            Return Me._hasHighSchoolDegree
        End Get
        Set(value As Boolean)
            Me.SetProperty(Me._hasHighSchoolDegree, value)
            OnPropertyChanged("HasTwoDegrees")
        End Set
    End Property
End Class
Public MustInherit Class BindableBase
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Protected Function SetProperty(Of T)(ByRef storage As T, value As T,
                                    Optional propertyName As String = Nothing) As Boolean

        If Object.Equals(storage, value) Then Return False

        storage = value
        Me.OnPropertyChanged(propertyName)
        Return True
    End Function
    Protected Sub OnPropertyChanged(Optional propertyName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

End Class

Since your child element is the one raising the PropertyChangeEvent and the count is bound to the parent property you need to subscribe to each child's PropertyChangedEvent within the parent. 由于您的子元素是引发PropertyChangeEvent并且count绑定到父属性的元素,因此您需要在父元素中订阅每个子元素的PropertyChangedEvent Then you need to raise your own property changed event within the parent with the PropertyName bound to the UI element. 然后,您需要在父元素中引发自己的属性更改事件,并将PropertyName绑定到UI元素。

The action you perform when the PropertyChangedEvent is raised is simply calling the OnPropertyChanged() method with the NumberOfChildrenWithDegrees string passed in. If you had a more complex object you may want to perform a case statement within ChildOnPropertyChanged() method based on the PropertyName in the event args. 该行动在执行时PropertyChangedEvent升高是简单地调用OnPropertyChanged()与方法NumberOfChildrenWithDegrees中传递的字符串。如果你有一个更复杂的对象,你可能要执行中的case语句ChildOnPropertyChanged()基础上,属性名的方法事件args。 I left a commented example in the code. 我在代码中留下了一个注释示例。 Here is the full C# code below, I didn't need to change the XAML. 下面是完整的C#代码,我不需要更改XAML。 Note that I also changed how the list was created and added to in order to subscribe to each child's property changed event upon adding them to the list. 请注意,我还更改了列表的创建方式和添加方式,以便在将每个子项的属性更改事件添加到列表后进行订阅。

using System.Linq;
using Microsoft.Phone.Controls;
using System.Collections.Generic;
using System.ComponentModel;

namespace PhoneApp1
{



public partial class TestPage : PhoneApplicationPage
{

    public TestPage()
    {
        InitializeComponent();
        this.parentLB.ItemsSource = Grandparent1().ParentGroups;
    }
    public GrandParent Grandparent1()
    {
        List<Parent> ParentGroups = new List<Parent>();
        ParentGroups.Add(Parent1());
        ParentGroups.Add(Parent2());
        GrandParent gp = new GrandParent();
        gp.ParentGroups = ParentGroups;
        return gp;
    }
    public Parent Parent2()
    {
        Parent p = new Parent { Name = "Tom" };
        Child c1 = new Child { Name = "Tammy" };
        Child c2 = new Child { Name = "Timmy" };
        p.AddChild(c1);
        p.AddChild(c2);

        return p;
    }

    public Parent Parent1()
    {
        Parent p = new Parent { Name = "Carol" };
        Child c1 = new Child { Name = "Carl" };
        c1.HasHighSchoolDegree = true;
        c1.HasUniversityDegree = true;
        Child c2 = new Child { Name = "Karla" };
        p.AddChild(c1);
        p.AddChild(c2);

        return p;
    }
}

public class GrandParent : BindableBase
{
    public List<Parent> ParentGroups { get; set; }
}
public class Parent : BindableBase
{
    public string Name { get; set; }



    private List<Child> _children;
    public List<Child> Children
    {
        get { return this._children; }
        set { _children = value; }
    }

    public Parent()
    {
        _children = new List<Child>();
    }

    public void AddChild(Child child)
    {
        child.PropertyChanged += ChildOnPropertyChanged;
        _children.Add(child);
    }

    private void ChildOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
    {
        //if(propertyChangedEventArgs.PropertyName == "HasUniversityDegree");
        OnPropertyChanged("NumberOfChildrenWithDegrees");
    }

    private int _numberOfChildrenWithDegrees;
    public int NumberOfChildrenWithDegrees
    {
        get { return Children.Where(f => f.HasTwoDegrees).Count(); }
        set { _numberOfChildrenWithDegrees = value; }
    }
}
public class Child : BindableBase
{
    public string Name { get; set; }

    public bool HasTwoDegrees
    {
        get { return HasHighSchoolDegree && HasUniversityDegree; }
    }

    private bool _hasUniversityDegree;
    public bool HasUniversityDegree
    {
        get { return this._hasUniversityDegree; }
        set
        {
            _hasUniversityDegree = value;
            OnPropertyChanged("HasTwoDegrees");
        }
    }

    private bool _hasHighSchoolDegree;
    public bool HasHighSchoolDegree
    {
        get { return this._hasHighSchoolDegree; }
        set
        {
            _hasHighSchoolDegree = value;
            OnPropertyChanged("HasTwoDegrees");
        }
    }
}
public abstract class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected bool SetProperty<T>(ref T storage, T value, string propertyName = null)
    {

        if (object.Equals(storage, value))
            return false;

        storage = value;
        this.OnPropertyChanged(propertyName);
        return true;
    }
    protected void OnPropertyChanged(string propertyName = null)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}
}

I hope this doesn't sound too much like an ad. 我希望这听起来不像广告。 With tools from the BCL, you only have the chance to do exactly what @jmshapland said. 使用BCL的工具,您只有机会完成@jmshapland所说的内容。 For this simple aggregate, this might be not too bad, but if the aggregates get more complex, the code rapidly gets more complex. 对于这个简单的聚合,这可能不是太糟糕,但如果聚合变得更复杂,代码会变得更加复杂。 However, there are tools that can support you with this, particularly my own tool: NMF Expressions (open source) 但是,有一些工具可以为您提供支持,尤其是我自己的工具: NMF Expressions (开源)

This tool basically allows you to create an observable expression and listen to its updates. 此工具基本上允许您创建可观察的表达式并监听其更新。

 public class Parent
 {
      private INotifyValue<bool> _allTwoDegrees;
      public Parent()
      {
          _allTwoDegrees = Observable.Expression(() => Children.WithUpdates().All(child => child.HasTwoDegrees));
          _allTwoDegrees.ValueChanged += (o,e) => OnPropertyChanged("AllTwoDegrees");
      }
      public bool AllTwoDegrees
      {
          get
          {
              return _allTwoDegrees.Value;
          }
      }
      ...
 }

A lot of aggregates are supported, including Sum, Count, Average, Min, Max, All and Any. 支持许多聚合,包括Sum,Count,Average,Min,Max,All和Any。 Furthermore, you can use almost all standard query operators. 此外,您几乎可以使用所有标准查询运算符。 However, this flexibility comes at a price, as the current implementation is based on Reflection. 然而,这种灵活性是有代价的,因为目前的实施基于反射。

You can drop the "WithUpdates()" method if you use VM classes that implement INotifyEnumerable directly, as done by a thin layer on top of ObservableCollection. 如果使用直接实现INotifyEnumerable的VM类,则可以删除“WithUpdates()”方法,如ObservableCollection顶部的薄层所做。

I've been trying to solve your problem for a little while and I must admit, that I've ended with very similar solution to @jmshapland - the principle is the same: subscribe to child's PropertyChanged . 我一直试图解决你的问题一段时间,我必须承认,我已经结束了与@jmshapland非常相似的解决方案 - 原理是一样的:订阅child的PropertyChanged In this situation I was wondering if to write this answer - at the end I've decided to do so, but treat this answer as a bigger comment to @jmshapland's solution. 在这种情况下,我想知道是否写这个答案 - 最后我决定这样做,但把这个答案视为对@ jmshapland解决方案的更大评论。 Here goes the code: 这里是代码:

Bindable class - making easier to implement INotify: Bindable类 - 更容易实现INotify:

public abstract class Bindable : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String property = null)
    {
        if (object.Equals(storage, value)) return false;
        storage = value;
        this.OnPropertyChanged(property);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string property = null)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}

Child class - probably nothing new: Child班 - 可能不是什么新鲜事:

public class Child : Bindable
{
    private string name;
    public string Name
    {
        get { return name; }
        set { SetProperty(ref name, value); }
    }

    public bool HasTwoDegrees { get { return HasHighSchoolDegree && HasUniversityDegree; } }

    private bool hasUniversityDegree;
    public bool HasUniversityDegree
    {
        get { return this.hasUniversityDegree; }
        set
        {
            SetProperty(ref hasUniversityDegree, value);
            OnPropertyChanged("HasTwoDegrees");
        }
    }

    private bool hasHighSchoolDegree;
    public bool HasHighSchoolDegree
    {
        get { return this.hasHighSchoolDegree; }
        set
        {
            SetProperty(ref hasHighSchoolDegree, value);
            OnPropertyChanged("HasTwoDegrees");
        }
    }
}

Parent class - here are some improvements - I subscribe to CollectionChanged event with a method that adds/removes subscription to added/removed items. Parent类 - 这里有一些改进 - 我使用一种方法订阅CollectionChanged事件,该方法添加/删除对添加/删除项的订阅。 The subscribed method item_PropertyChanged checks if the property that invoked it, is allowed to update Parent's properties. 订阅方法item_PropertyChanged检查是否允许调用它的属性更新Parent的属性。 If so then OnPropertyChanged event is raised for each of defined bindingNames . 如果是,则为每个已定义的bindingNames引发OnPropertyChanged事件。 I think it will be easier if you just look at the code: 我认为如果你只看代码会更容易:

public class Parent : Bindable
{       
    private string[] bindingNames;
    private string[] allowedProperties;

    private string name;
    public string Name
    {
        get { return name; }
        set { SetProperty(ref name, value); }
    }

    private ObservableCollection<Child> children = new ObservableCollection<Child>();
    public ObservableCollection<Child> Children
    {
        get { return this.children; }
        set { children = value; }
    }

    public Parent()
    {
        this.children.CollectionChanged += children_CollectionChanged;
        bindingNames = new string[] { "NumberOfChildrenWithDegrees" };
        allowedProperties = new string[] { "HasUniversityDegree", "HasHighSchoolDegree" };
    }

    private void children_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
            foreach (Object item in e.NewItems)
                if (item is INotifyPropertyChanged)
                    (item as INotifyPropertyChanged).PropertyChanged += item_PropertyChanged;
        if (e.OldItems != null)
            foreach (Object item in e.OldItems)
                (item as INotifyPropertyChanged).PropertyChanged -= item_PropertyChanged;
    }

    private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (bindingNames != null)
            foreach (string item in bindingNames)
                if (allowedProperties.Contains(e.PropertyName))
                    OnPropertyChanged(item);
    }

    public int NumberOfChildrenWithDegrees
    {
        get { return Children.Where(f => f.HasTwoDegrees).Count(); }
    }
}

The MainPage class: (I've not changed the xaml file) MainPage类:(我没有更改xaml文件)

public partial class MainPage : PhoneApplicationPage
{
    ObservableCollection<Parent> parentGroups = new ObservableCollection<Parent>();

    public ObservableCollection<Parent> ParentGroups
    {
        get { return parentGroups; }
        set { parentGroups = value; }
    }

    public MainPage()
    {
        InitializeComponent();
        this.parentLB.DataContext = this;
        FillParents();
    }

    public void FillParents()
    {
        Parent p = new Parent { Name = "Tom" };
        Child c1 = new Child { Name = "Tammy" };
        Child c2 = new Child { Name = "Timmy" };
        p.Children.Add(c1);
        p.Children.Add(c2);
        ParentGroups.Add(p);
        p = new Parent { Name = "Carol" };
        c1 = new Child { Name = "Carl" };
        c1.HasHighSchoolDegree = true;
        c1.HasUniversityDegree = true;
        c2 = new Child { Name = "Karla" };
        p.Children.Add(c1);
        p.Children.Add(c2);
        ParentGroups.Add(p);
    }
}

Maybe it will help a little. 也许它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM