Repository navigation
Expand file tree
/
Copy pathARPTableView.xaml.cs
More file actions
101 lines (84 loc) · 2.87 KB
/
Copy pathARPTableView.xaml.cs
File metadata and controls
101 lines (84 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using NETworkManager.Models.Network;
using NETworkManager.Utilities;
using NETworkManager.ViewModels;
namespace NETworkManager.Views;
public partial class ARPTableView
{
private readonly ARPTableViewModel _viewModel = new();
public ARPTableView()
{
InitializeComponent();
DataContext = _viewModel;
}
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
if (sender is ContextMenu menu)
menu.DataContext = _viewModel;
}
public void OnViewHide()
{
_viewModel.OnViewHide();
}
public void OnViewVisible()
{
_viewModel.OnViewVisible();
}
private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e)
{
var column = e.Column;
var selectedComparer = -1;
switch (column.SortMemberPath)
{
case nameof(ARPInfo.IPAddress):
selectedComparer = 0;
break;
case nameof(ARPInfo.MACAddress):
selectedComparer = 1;
break;
default:
return;
}
// Prevent the built-in sort from sorting
e.Handled = true;
// Get the direction
var direction = column.SortDirection != ListSortDirection.Ascending
? ListSortDirection.Ascending
: ListSortDirection.Descending;
// Update the sort direction
e.Column.SortDirection = direction;
// Get the view
var view = (ListCollectionView)CollectionViewSource.GetDefaultView(((DataGrid)sender).ItemsSource);
// Sort the view
view.CustomSort = new DataGridComparer(direction, selectedComparer);
}
public class DataGridComparer(ListSortDirection direction, int comparer = -1) : IComparer
{
public int Compare(object x, object y)
{
// No comparer selected
if (comparer == -1)
return 0;
// Get data from objects
if (x is not ARPInfo first || y is not ARPInfo second)
return 0;
// Compare the data
return comparer switch
{
// IP address
0 => direction == ListSortDirection.Ascending
? IPAddressHelper.CompareIPAddresses(first.IPAddress, second.IPAddress)
: IPAddressHelper.CompareIPAddresses(second.IPAddress, first.IPAddress),
// MAC address
1 => direction == ListSortDirection.Ascending
? MACAddressHelper.CompareMACAddresses(first.MACAddress, second.MACAddress)
: MACAddressHelper.CompareMACAddresses(second.MACAddress, first.MACAddress),
_ => 0
};
}
}
}