Joining 2 collections into datagrid using multibin

2019-07-14 23:15发布

问题:

I have 2 collections of data, the first one is only for group names and the second one is for values.

I want to fill these data in a datagrid like this:

groupId  |  group  | store 1 | store 2 | store 3 | store 4
   1     |  grp 1  |         |         |         |
   2     |  grp 2  |         |         |         |
   3     |  grp 3  |         |         |         |
   4     |  grp 4  |         |         |         |

I created a new property in the modelview which creating a new collection of the required rows.

Then I tried to achieve it using multibinding and converters like this.

<DataGrid>
   <DataGrid.Columns>
      <DataGridTextColumn Header="groupId" Binding="{Binding groupId}" IsReadOnly="True" />
      <DataGridTextColumn Header="group" Binding="{Binding groupName}" IsReadOnly="True" />
      <DataGridTextColumn Header="store 1" Binding="{Binding s1}" />
      <DataGridTextColumn Header="store 2" Binding="{Binding s2}" />
      <DataGridTextColumn Header="store 3" Binding="{Binding s3}" />
      <DataGridTextColumn Header="store 4" Binding="{Binding s4}" />
    </DataGrid.Columns>

    <MultiBinding Converter="{StaticResource GroupsToValuesConverter}">
       <Binding Path="Groups"></Binding>
       <Binding Path="Values"></Binding>
    </MultiBinding>
</DataGrid>

But got this error.

Additional information: A 'MultiBinding' cannot be used within a 'ItemCollection' collection. A 'MultiBinding' can only be set on a DependencyProperty of a DependencyObject.

I believe it can be done by this way but missing a step here.

I am so new for WPF so I appreciate your help.

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    ObservableCollection<GroupStoreRow> retValue = new ObservableCollection<GroupStoreRow>();

    IEnumerable<Group> groups = (IEnumerable<Group>)values[0];
    Store o = (Store)values[1];
    foreach (Person group in groups)
    {
        int GroupId = (int)group.GroupId;
        GroupStoreRow c = new GroupStoreRow(GroupId, group.Name, group.SortOrder);
        if (o != null)
        {
            if (o.Store1.ContainsKey(groupId))
            {
                c.Amount1 = (int)o.Store1[groupId].Amount ;
                c.Capacity1 = (int)o.Store1[groupId].Capacity ;
            }
            if (o.Store2.ContainsKey(groupId))
            {
                c.Amount2 = (int)o.Store2[groupId].Amount ;
                c.Capacity2 = (int)o.Store2[groupId].Capacity ;
            }
            if (o.Store3.ContainsKey(groupId))
            {
                c.Amount3 = (int)o.Store3[groupId].Amount ;
                c.Capacity3 = (int)o.Store3[groupId].Capacity ;
            }
        }
        retValue.Add(c);
    }
    return retValue;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
    throw new NotImplementedException();
}

and this GroupStoreRow class

class GroupStoreRow : BindableBase
{

    private bool hasChanged = false;
    private int _amount1, _capacity1;
    private int _amount2, _capacity2;
    private int _amount3, _capacity3;


    public int GroupID { get; private set; }
    public string GroupName { get; private set; }
    public int SortOrder { get; private set; }

    public GroupStoreRow(int groupId, string groupName, int sortOrder)
    {
        GroupID = groupId;
        GroupName = groupName;
        SortOrder = sortOrder;
    }

    public int Amount1
    {
        get { return _amount1; }
        set
        {
            SetProperty(ref _amount1, value);
        }
    }
    public decimal Capacity1
    {
        get { return _capacity1; }
        set
        {
            SetProperty(ref _capacity1, value);
        }
    }
    ...
    public bool Changed
    {
        get
        {
            return hasChanged;
        }
    }

    protected override bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        bool returnValue = base.SetProperty<T>(ref storage, value, propertyName);
        if (returnValue)
        {
            if (!hasChanged)
                hasChanged = true;
        }
        return returnValue;
    }
}

回答1:

You can set ItemsSource with MultiBinding. So you can write something like this.

<DataGrid>
    <DataGrid.ItemsSource>
        <MultiBinding Converter="{StaticResource MyMultiValConverter}">
            <Binding Path="Objects" />
            <Binding Path="Objects2" />
        </MultiBinding>
    </DataGrid.ItemsSource>
</DataGrid>

And you should carefully write MultiValuesConverter to achive desired effect. For example it can be like this (just joining two collections). Where OwnObject is my viewModel.

public class DGMultiValueConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert( object[] values, Type targetType, object parameter, 
                           CultureInfo culture )
    {
        ObservableCollection<OwnObject> combinedCollection = 
           new ObservableCollection<OwnObject>();
        foreach ( var item in values[0] as ObservableCollection<OwnObject> )
        {
            combinedCollection.Add( item );
        }
        foreach ( var item in values[1] as ObservableCollection<OwnObject> )
        {
            combinedCollection.Add( item );
        }
        return combinedCollection;
    }

    public object[] ConvertBack( object value, Type[] targetTypes, object parameter,
                                 CultureInfo culture )
    {
        throw new NotImplementedException();
    }

    #endregion
}