簡體   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