我正在尝试将通用转换器从 DataGrid.SelectedItems 制作为通用列表
public class SelectedItemsToListConverter : BaseConverter, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DataGrid dg = (DataGrid)parameter;
Type type = parameter.GetType().MemberType.GetType();
return dg.SelectedItems.OfType<type.GetType()>().ToList();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
在object
参数中我传递了整个DataGrid
,希望可以得到基本类型ItemsSource
但是当尝试
dg.SelectedItems.OfType<type.GetType()>().ToList();
或者
dg.SelectedItems.OfType<type>().ToList();
它不识别类型。
如何获取类型并将其传递给dg.SelectedItems.OfType<T>
?
是否已经有通用转换器来执行此操作?
注意:我需要转换SelectedItems
以将其用作 的参数ICommand
,我的命令需要 a List<T>
,然后我的转换器必须将SelectedItems转换为List<T>
<DataGrid x:Name="CausacionesDisponibles" GridLinesVisibility="Vertical" SelectionUnit="FullRow" ItemsSource="{Binding CausacionesRealizadasAlTercero}" IsReadOnly="True" AutoGenerateColumns="False">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding DetallarCausacionCommand}" CommandParameter="{Binding ElementName=CausacionesDisponibles,Path=SelectedItems}" HERE NEED TO USE THE CONVERTER/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding StrIdDocumento}" Header="{x:Static resources:Labels.GENERAL_IdDocumeto}"/>
<DataGridTextColumn Binding="{Binding DatFechaDocumento}" Header="{x:Static resources:Labels.GENERAL_FechaDocumento}"/>
<DataGridTextColumn Binding="{Binding StrUsuarioGenera,StringFormat=\{0:d\}}" Header="{x:Static resources:Labels.GENERAL_GeneradoPor}"/>
</DataGrid.Columns>
</DataGrid>