ListView 中的上下文菜单,选择一个项目
Context Menu in ListView, choosing an Item
我希望在我的 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
class SearchFolder
{
public string Name { get; set; }
public string Folder { get; set; }
public string Location { get; set; }
}
如何创建上下文菜单,其中对 ListView 中单击的项目执行操作(复制路径或打开文件夹)?
/edit:这样,无论我在何处右键单击,上下文菜单都会显示。但是我找不到访问点击项目的方法,这不起作用:
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);
}
}
}
ListView 有一个事件 "SelectionChanged"。使用这个事件你可以在这个事件中设置你的所有代码并访问发件人中的一个项目。
代码:
<ListBox Grid.Row="1" Grid.Column="1" Margin="10,10,0,0" x:Name="lstBank" Style="{StaticResource TransparentListBox}" ItemTemplate="{StaticResource BankDataTemplate}"
MinHeight="35" MaxHeight="220" SelectionMode="Single" ItemsSource="{Binding Path=Banks}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="lstBank_SelectionChanged" />
并在 class 文件中设置代码。
private void lstBank_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {
}
在您的点击事件处理程序上试试这个:
private void ItemRightClick(object sender, EventArgs e)
{
MenuItem item = sender as MenuItem;
if (item!= null)
{
ContextMenu contextMenu = item.Parent;
Control clickedControl = contextMenu.SourceControl;
}
}
没看过...
无论如何,另一种方法是使用 'MouseDown' 事件并检查点击的按钮是否正确:
private void ItemMouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
ListViewItem item = listView.GetItemAt(e.X, e.Y);
if(item != null)
{
// here we have
}
}
}
我终于做到了我想做的事,我保留了以前的上下文菜单,并且能够在 ContextMenu 中的 MenuItems 的事件处理程序中使用以下代码获取该项目
if(resultsView.SelectedIndex>-1)
{
SearchFolder selectedItem;
selectedItem = (SearchFolder)resultsView.SelectedItem;
//do stuff with selectedItem
}
我希望在我的 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
class SearchFolder
{
public string Name { get; set; }
public string Folder { get; set; }
public string Location { get; set; }
}
如何创建上下文菜单,其中对 ListView 中单击的项目执行操作(复制路径或打开文件夹)?
/edit:这样,无论我在何处右键单击,上下文菜单都会显示。但是我找不到访问点击项目的方法,这不起作用:
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);
}
}
}
ListView 有一个事件 "SelectionChanged"。使用这个事件你可以在这个事件中设置你的所有代码并访问发件人中的一个项目。
代码:
<ListBox Grid.Row="1" Grid.Column="1" Margin="10,10,0,0" x:Name="lstBank" Style="{StaticResource TransparentListBox}" ItemTemplate="{StaticResource BankDataTemplate}"
MinHeight="35" MaxHeight="220" SelectionMode="Single" ItemsSource="{Binding Path=Banks}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="lstBank_SelectionChanged" />
并在 class 文件中设置代码。
private void lstBank_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {
}
在您的点击事件处理程序上试试这个:
private void ItemRightClick(object sender, EventArgs e)
{
MenuItem item = sender as MenuItem;
if (item!= null)
{
ContextMenu contextMenu = item.Parent;
Control clickedControl = contextMenu.SourceControl;
}
}
没看过...
无论如何,另一种方法是使用 'MouseDown' 事件并检查点击的按钮是否正确:
private void ItemMouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
ListViewItem item = listView.GetItemAt(e.X, e.Y);
if(item != null)
{
// here we have
}
}
}
我终于做到了我想做的事,我保留了以前的上下文菜单,并且能够在 ContextMenu 中的 MenuItems 的事件处理程序中使用以下代码获取该项目
if(resultsView.SelectedIndex>-1)
{
SearchFolder selectedItem;
selectedItem = (SearchFolder)resultsView.SelectedItem;
//do stuff with selectedItem
}