簡體   English   中英

在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 SearchFolder
{
    public string Name { get; set; }

    public string Folder { get; set; }

    public string Location { get; set; }
}

如何創建一個ContextMenu,在該菜單上對ListView中的單擊項執行操作(復制路徑或打開的文件夾)?

/ edit:這樣,無論我在哪里單擊鼠標右鍵,都將顯示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);
                }
            }
        }

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" />

並在類文件中設置代碼。

 private void lstBank_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {

        }

我終於設法完成了我想做的事情,我像以前一樣離開了上下文菜單,並且能夠在ContextMenu中的MenuItems的事件處理程序中使用以下代碼獲取該項目

if(resultsView.SelectedIndex>-1)
        {
            SearchFolder selectedItem;
            selectedItem = (SearchFolder)resultsView.SelectedItem;
            //do stuff with selectedItem
        }

在點擊事件處理程序上嘗試以下操作:

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
          }
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM