多重绑定数据网格所选项目不起作用
我使用以下方法从数据网格中获取选定的项目,它工作正常。
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,从你的转换器的代码来看,这绝对行不通。
你不能简单地写
return Values;
,你需要做更多的事情。我同意这一点:
重点是你的 commandParameters 想要一个集合,而你给他一个包含 2 个集合的集合。因此,您需要将这两个集合合并为一个集合才能使其正常工作。
编辑:只是为了好玩,这里是使用 Linq 的代码:(
您可能已经/想要用项目的真实类型替换此处的 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:
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:
(you might have/want to replace the 2 "
<object>
" here by the real Types of your items)