多重绑定数据网格所选项目不起作用

发布于 2024-10-31 20:18:32 字数 1266 浏览 4 评论 0原文

我使用以下方法从数据网格中获取选定的项目,它工作正常。

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

现在我有一个命令需要来自 2 个数据网格的 2 个选定项目列表。因此,我尝试了以下多重绑定:

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

我的 converter.Convert() 方法在初始化时调用一次,但 CommandParameter 始终为 null。也许我错过了一些东西......

编辑:grdDruckVersand 和 grdAusgabe 是 DataGrids

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

转换器:

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2024-11-07 20:18:32

好吧,从你的转换器的代码来看,这绝对行不通。

你不能简单地写return Values;,你需要做更多的事情。

我同意这一点:

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

}

重点是你的 commandParameters 想要一个集合,而你给他一个包含 2 个集合的集合。因此,您需要将这两个集合合并为一个集合才能使其正常工作。

编辑:只是为了好玩,这里是使用 Linq 的代码:(

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

您可能已经/想要用项目的真实类型替换此处的 2 个“”)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文