开发者

multibinding datagrid selecteditems don't work

开发者 https://www.devze.com 2023-02-24 07:15 出处:网络
I use the following to get the selecteditems from a datagrid and it works fine. <Button Command=\"{Binding DeleteDataCommand}\"

I use the following to get the selecteditems from a datagrid and it works fine.

<Button Command="{Binding DeleteDataCommand}" 
        CommandParameter="{Binding ElementName=MyGridCtrl, Path=SelectedItems}"/>

Now I have a command that needs 2 lists开发者_如何学JAVA of selecteditems from 2 datagrids. So I tried the following multibinding:

<Button.CommandParameter>
    <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
        <Binding ElementName="grdDruckVersand" Path="SelectedItems"/>
        <Binding ElementName="grdAusgabe" Path="SelectedItems"/>
     </MultiBinding>
 </Button.CommandParameter>

my converter.Convert() method is called once on initialization, but CommandParameter is always null. Maybe I'm missing something...

EDIT: grdDruckVersand and grdAusgabe are DataGrids

 <DataGrid x:Name="grdDruckVersand " ...
 <DataGrid x:Name="grdAusgabe " ...

Converter:

public class MultiValueConverter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values;
    }


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

}


well, from the code of your converter, this can definitely not work.

you cannot simply write return Values;, you need to do a bit more.

I'd go with this:

public class MultiValueConverter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        // Caution: overdetailed code following:

        var itemsToDeleteFromGrdDruckVersand = values[0]
        var itemsToDeleteFromGrdAusgabe = values[1]

        var itemsToDelete = itemsToDeleteFromGrdDruckVersand;

        foreach (var item in itemsToDeleteFromGrdAusgabe)
        {
           itemsToDelete.Add(item);
        }
        // you can do a lot better with Linq if you want

        return itemsToDelete;
    }


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

}

the point is that your commandParameters wants one collection and you're giving him a collection of 2 collections. So you need to merge those 2 collections into one to get it to work.

edit: just for the fun, here would be the code using Linq:

return ((Collection<object>)values[0]).Concat((Collection<object>)values[1]);

(you might have/want to replace the 2 "<object>" here by the real Types of your items)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号