我想有一個右鍵菜單在我的ListView單擊某個項目時上下文菜單中的ListView,選擇一個項目
<ListView x:Name="resultsView" Grid.Column="1" Margin="10" Grid.Row="1" FontFamily="Verdana" FontSize="14">
<ListView.ContextMenu>
<ContextMenu FontFamily="Verdana" FontSize="14">
<MenuItem Header="Open Folder" Click="openFolder_Click" FontFamily="Verdana" FontSize="14"/>
<Separator/>
<MenuItem Header="Copy Path" Click="copyPath_Click" FontFamily="Verdana" FontSize="14"/>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView x:Name="gridView">
<GridViewColumn x:Name="name" Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn x:Name="folder" Header="Folder" DisplayMemberBinding="{Binding Folder}"/>
<GridViewColumn x:Name="location" Header="Path" DisplayMemberBinding="{Binding Location}"/>
</GridView>
</ListView.View>
</ListView>
這是我如何將項目添加到我的ListView(結果是SearchFolder的公司的列表):
results.Add(new SearchFolder { Name = Path.GetFileName(directory), Folder = Path.GetFileName(Path.GetDirectoryName(directory)), Location = directory });
resultsView.ItemsSource = results;
而且我SearchFolder的類
class SearchFolder
{
public string Name { get; set; }
public string Folder { get; set; }
public string Location { get; set; }
}
如何創建一個文本菜單,其中的操作(複製路徑或OPE n文件夾)在ListView中點擊的項目上執行?
/編輯:這樣,無論在哪裏右鍵單擊,ContextMenu都會顯示出來。但我不能找到一種方法來訪問點擊項目,這不起作用:
MenuItem temp = sender as MenuItem;
if (temp != null)
{
ContextMenu anotherTemp = temp.Parent as ContextMenu;
if (anotherTemp != null)
{
ListViewItem clickedItem = anotherTemp.PlacementTarget as ListViewItem;
if (clickedItem != null)
{
Console.WriteLine("Copy Path " + clickedItem.Name);
}
}
}
比預期容易得多,很好! –