Xaml

<Window x:Class="PortScanner.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Scanner Port" WindowStartupLocation="CenterScreen"> <ListView Name="listview_scaner" Margin="10"> <ListView.Resources> <Style TargetType="{x:Type ListView}"> <Setter Property="ItemContainerStyle"> <Setter.Value> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Center"/> </Style> </Setter.Value> </Setter> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu> <MenuItem Header="Скопировать в буфер обмена"></MenuItem> </ContextMenu> </Setter.Value> </Setter> </Style> </ListView.Resources> <ListView.View> <GridView> <GridView.Columns> <GridViewColumn Header="Port ID" DisplayMemberBinding="{Binding Path=PortNumber}" Width="150"/> <GridViewColumn Header="Local Adress" DisplayMemberBinding="{Binding Path=Local}" Width="250"/> <GridViewColumn Header="Remote Adress" DisplayMemberBinding="{Binding Path=Remote}" Width="250"/> <GridViewColumn Header="State" DisplayMemberBinding="{Binding Path=State}" Width="250"/> </GridView.Columns> </GridView> </ListView.View> </ListView> </Window> 

I created a context menu, but I can’t figure out how to make sure that when I press a button, I copy the necessary text from the required column to the clipboard

Code:

 using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Windows; using System.Windows.Controls; namespace PortScanner { public partial class MainWindow : Window { public MainWindow() { this.InitializeComponent(); // this.CenterWindowOnScreen(); this.listview_scaner.ItemsSource = GetOpenPort(); } private static List<PortInfo> GetOpenPort() { var properties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners(); return properties.GetActiveTcpConnections().Select(p => { return new PortInfo(p.LocalEndPoint.Port, $"{p.LocalEndPoint.Address}:{p.LocalEndPoint.Port}", $"{p.RemoteEndPoint.Address}:{p.RemoteEndPoint.Port}", p.State.ToString()); }).ToList(); } private void CenterWindowOnScreen() { double screenWidth = SystemParameters.PrimaryScreenWidth; double screenHeight = SystemParameters.PrimaryScreenHeight; double windowWidth = this.Width; double windowHeight = this.Height; this.Left = (screenWidth / 2) - (windowWidth / 2); this.Top = (screenHeight / 2) - (windowHeight / 2); } private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { foreach (ListViewItem item in e.AddedItems.OfType<ListViewItem>()) { Clipboard.SetText(item?.ToString()); } } catch { } } } } 
  • Well, use the SelectedItem property. And then loop through all that is not necessary. - LLENN
  • @LLENN, To be honest, I’m writing to wpf for the first time, and I’d like to know as an example where to put this property SelectedItem - Luser

1 answer 1

Use the ListView.SelectedItem property, which in your case will store an instance of the selected PortInfo :

XAML:

 <ListView.ContextMenu> <ContextMenu> <MenuItem Header="Скопировать в буфер обмена" Click="MenuItem_Click"/> </ContextMenu> </ListView.ContextMenu> 

Code-behind:

 private void MenuItem_Click(object sender, RoutedEventArgs e) { if (listview_scaner.SelectedItem != null) { Clipboard.SetText(listview_scaner.SelectedItem.ToString()); } } 
  • Thanks, why don’t you use null check in this form Clipboard.SetText(listview_scaner.SelectedItem?.ToString()); - Luser pm
  • Because Clipboard.SetText throw an ArgumentNullException if it is passed null . - Raider