繁体   English   中英

如何识别在datagrid wpf中选择的文件列表?

[英]How to identify the list of files that are selected in a datagrid wpf?

我有一个具有文件列表的datagrid。 为了允许多选,我选择了SelectionMode =“ Extended”。 我可以使用Ctrl或Shift键选择多个文件。

如何识别所选文件列表?

使用它可以从数据网格中获取选定的项目。

List<FilesData> filesList;
for (int i = 0; i < dataGridName.SelectedItems.Count; i++)
{
    filesList.Add((FilesData)dataGridName.SelectedItems[i]);
}

您可以检查此主题以查看绑定到SelectedItems属性的一些实现。

基本上,有两种流行的解决方案。 一种是从DataGrid派生并实现自己的依赖项属性,该属性将公开SelectedItems属性,以便可以将其用于数据绑定:

public class CustomDataGrid : DataGrid{

public CustomDataGrid ()
{
    this.SelectionChanged += CustomDataGrid_SelectionChanged;
}

void CustomDataGrid_SelectionChanged (object sender, SelectionChangedEventArgs e)
{
    this.SelectedItemsList = this.SelectedItems;
}

public IList SelectedItemsList
{
    get { return (IList)GetValue (SelectedItemsListProperty); }
    set { SetValue (SelectedItemsListProperty, value); }
}

public static readonly DependencyProperty SelectedItemsListProperty =
        DependencyProperty.Register ("SelectedItemsList", typeof (IList), typeof (CustomDataGrid), new PropertyMetadata (null));
}

其他方法是使用System.Windows.Interactivity.dll程序集中提供的功能,并创建自己的行为,或者在发生SelectionChanged事件时使用提供的触发器和调用命令:

<i:Interaction.Triggers>
 <i:EventTrigger EventName="SelectionChanged">
     <i:InvokeCommandAction Command="{Binding SelectItemsCommand}" CommandParameter="{Binding Path=SelectedItems,ElementName=YourDataGridName}"/>
 </i:EventTrigger>
</i:Interaction.Triggers>

请记住,要使用此功能,您必须像这样在xaml中定义“ i”名称空间:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM