使用多绑定将2个集合连接到数据网格中

时间:2016-03-10 07:51:50

标签: c# wpf ivalueconverter multibinding

我有2个数据集合,第一个仅用于组名,第二个用于值。

我想在这样的数据网格中填充这些数据:

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

我在modelview中创建了一个新属性,用于创建所需行的新集合。

然后我尝试使用像这样的多绑定和转换器来实现它。

<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>

但是得到了这个错误。

  

附加信息:'MultiBinding'不能在a中使用   'ItemCollection'集合。 'MultiBinding'只能在a上设置   DependencyObject的DependencyProperty。

我相信可以通过这种方式完成,但却错过了一步。

我对WPF很新,所以感谢你的帮助。

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

和这个GroupStoreRow类

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 个答案:

答案 0 :(得分:3)

您可以使用ItemsSource设置MultiBinding。所以你可以写这样的东西。

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

你应该仔细写MultiValuesConverter以达到预期的效果。例如,它可以是这样的(只加入两个集合)。其中OwnObject是我的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
}