Skip to content

Commit e15bbe6

Browse files
committed
Feature: SNMP dialog
1 parent 0a34a7a commit e15bbe6

File tree

6 files changed

+95
-82
lines changed

6 files changed

+95
-82
lines changed

Source/NETworkManager/ViewModels/SNMPViewModel.cs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using log4net;
22
using MahApps.Metro.Controls;
3-
using MahApps.Metro.Controls.Dialogs;
43
using MahApps.Metro.SimpleChildWindow;
54
using NETworkManager.Controls;
65
using NETworkManager.Localization;
@@ -31,12 +30,10 @@ public class SNMPViewModel : ViewModelBase
3130
{
3231
#region Contructor, load settings
3332

34-
public SNMPViewModel(IDialogCoordinator instance, Guid tabId, SNMPSessionInfo sessionInfo)
33+
public SNMPViewModel(Guid tabId, SNMPSessionInfo sessionInfo)
3534
{
3635
_isLoading = true;
3736

38-
_dialogCoordinator = instance;
39-
4037
ConfigurationManager.Current.SNMPTabCount++;
4138

4239
_tabId = tabId;
@@ -57,36 +54,34 @@ public SNMPViewModel(IDialogCoordinator instance, Guid tabId, SNMPSessionInfo se
5754
Oid = sessionInfo?.OID;
5855

5956
// Modes
60-
Modes = new List<SNMPMode> { SNMPMode.Get, SNMPMode.Walk, SNMPMode.Set };
57+
Modes = [SNMPMode.Get, SNMPMode.Walk, SNMPMode.Set];
6158
Mode = Modes.FirstOrDefault(x => x == sessionInfo?.Mode);
6259

6360
// Versions (v1, v2c, v3)
64-
Versions = Enum.GetValues(typeof(SNMPVersion)).Cast<SNMPVersion>().ToList();
61+
Versions = Enum.GetValues<SNMPVersion>().Cast<SNMPVersion>().ToList();
6562
Version = Versions.FirstOrDefault(x => x == sessionInfo?.Version);
6663

6764
// Community
6865
if (Version != SNMPVersion.V3)
6966
Community = sessionInfo?.Community;
7067

7168
// Security
72-
Securities = new List<SNMPV3Security>
73-
{ SNMPV3Security.NoAuthNoPriv, SNMPV3Security.AuthNoPriv, SNMPV3Security.AuthPriv };
69+
Securities = [SNMPV3Security.NoAuthNoPriv, SNMPV3Security.AuthNoPriv, SNMPV3Security.AuthPriv];
7470
Security = Securities.FirstOrDefault(x => x == sessionInfo?.Security);
7571

7672
// Username
7773
if (Version == SNMPVersion.V3)
7874
Username = sessionInfo?.Username;
7975

8076
// Auth
81-
AuthenticationProviders = Enum.GetValues(typeof(SNMPV3AuthenticationProvider))
82-
.Cast<SNMPV3AuthenticationProvider>().ToList();
77+
AuthenticationProviders = [.. Enum.GetValues<SNMPV3AuthenticationProvider>().Cast<SNMPV3AuthenticationProvider>()];
8378
AuthenticationProvider = AuthenticationProviders.FirstOrDefault(x => x == sessionInfo?.AuthenticationProvider);
8479

8580
if (Version == SNMPVersion.V3 && Security != SNMPV3Security.NoAuthNoPriv)
8681
Auth = sessionInfo?.Auth;
8782

8883
// Priv
89-
PrivacyProviders = Enum.GetValues(typeof(SNMPV3PrivacyProvider)).Cast<SNMPV3PrivacyProvider>().ToList();
84+
PrivacyProviders = [.. Enum.GetValues<SNMPV3PrivacyProvider>().Cast<SNMPV3PrivacyProvider>()];
9085
PrivacyProvider = PrivacyProviders.FirstOrDefault(x => x == sessionInfo?.PrivacyProvider);
9186

9287
if (Version == SNMPVersion.V3 && Security == SNMPV3Security.AuthPriv)
@@ -100,8 +95,6 @@ public SNMPViewModel(IDialogCoordinator instance, Guid tabId, SNMPSessionInfo se
10095
#region Variables
10196
private static readonly ILog Log = LogManager.GetLogger(typeof(SNMPViewModel));
10297

103-
private readonly IDialogCoordinator _dialogCoordinator;
104-
10598
private CancellationTokenSource _cancellationTokenSource;
10699

107100
private readonly Guid _tabId;
@@ -656,25 +649,27 @@ private async Task OpenOIDProfileSelection()
656649
{
657650
var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
658651

659-
var customDialog = new CustomDialog
660-
{
661-
Title = Strings.SelectOIDProfile
662-
};
652+
var childWindow = new SNMPOIDProfilesChildWindow(window);
663653

664-
var viewModel = new SNMPOIDProfilesViewModel(async instance =>
654+
var childWindowViewModel = new SNMPOIDProfilesViewModel(async instance =>
665655
{
666-
await _dialogCoordinator.HideMetroDialogAsync(window, customDialog);
656+
childWindow.IsOpen = false;
657+
ConfigurationManager.Current.IsChildWindowOpen = false;
667658

668659
Mode = instance.SelectedOIDProfile.Mode;
669660
Oid = instance.SelectedOIDProfile.OID;
670-
}, async _ => { await _dialogCoordinator.HideMetroDialogAsync(window, customDialog); });
661+
}, async _ => {
662+
childWindow.IsOpen = false;
663+
ConfigurationManager.Current.IsChildWindowOpen = false;
664+
});
671665

672-
customDialog.Content = new SNMPOIDProfilesDialog
673-
{
674-
DataContext = viewModel
675-
};
666+
childWindow.Title = Strings.SelectOIDProfile;
667+
668+
childWindow.DataContext = childWindowViewModel;
669+
670+
ConfigurationManager.Current.IsChildWindowOpen = true;
676671

677-
await _dialogCoordinator.ShowMetroDialogAsync(window, customDialog);
672+
await window.ShowChildWindowAsync(childWindow);
678673
}
679674

680675
private void AddHostToHistory(string host)

Source/NETworkManager/Views/SNMPOIDProfilesDialog.xaml renamed to Source/NETworkManager/Views/SNMPOIDProfilesChildWindow.xaml

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
<UserControl x:Class="NETworkManager.Views.SNMPOIDProfilesDialog"
1+
<simpleChildWindow:ChildWindow x:Class="NETworkManager.Views.SNMPOIDProfilesChildWindow"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:network="clr-namespace:NETworkManager.Models.Network;assembly=NETworkManager.Models"
77
xmlns:viewModels="clr-namespace:NETworkManager.ViewModels"
88
xmlns:localization="clr-namespace:NETworkManager.Localization.Resources;assembly=NETworkManager.Localization"
9-
mc:Ignorable="d" Loaded="UserControl_Loaded"
10-
d:DataContext="{d:DesignInstance viewModels:SNMPOIDProfilesViewModel}">
11-
<Grid Margin="0,20">
9+
xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"
10+
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
11+
CloseButtonCommand="{Binding Path=CancelCommand}"
12+
Style="{StaticResource DefaultChildWindow}"
13+
Loaded="ChildWindow_OnLoaded"
14+
mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:SNMPOIDProfilesViewModel}">
15+
<Grid Margin="10">
1216
<Grid.RowDefinitions>
1317
<RowDefinition Height="Auto" />
1418
<RowDefinition Height="10" />
@@ -17,53 +21,53 @@
1721
<RowDefinition Height="Auto" />
1822
</Grid.RowDefinitions>
1923
<TextBox x:Name="TextBoxSearch"
20-
Grid.Column="0" Grid.Row="0"
21-
VerticalAlignment="Center"
22-
Text="{Binding Path=Search, UpdateSourceTrigger=PropertyChanged}"
23-
Style="{StaticResource ResourceKey=SearchTextBox}" />
24+
Grid.Column="0" Grid.Row="0"
25+
VerticalAlignment="Center"
26+
Text="{Binding Path=Search, UpdateSourceTrigger=PropertyChanged}"
27+
Style="{StaticResource ResourceKey=SearchTextBox}" />
2428
<Grid Grid.Column="0" Grid.Row="2">
2529
<DataGrid x:Name="DataGridProfiles"
26-
ItemsSource="{Binding Path=OIDProfiles}"
27-
SelectedItem="{Binding Path=SelectedOIDProfile}"
28-
SelectionMode="Single">
30+
ItemsSource="{Binding Path=OIDProfiles}"
31+
SelectedItem="{Binding Path=SelectedOIDProfile}"
32+
SelectionMode="Single">
2933
<DataGrid.Columns>
3034
<DataGridTextColumn Header="{x:Static Member=localization:Strings.Name}"
31-
Binding="{Binding Path=(network:SNMPOIDProfileInfo.Name)}"
32-
SortMemberPath="Name"
33-
Width="1*" />
35+
Binding="{Binding Path=(network:SNMPOIDProfileInfo.Name)}"
36+
SortMemberPath="Name"
37+
Width="1*" />
3438
<DataGridTextColumn Header="{x:Static Member=localization:Strings.OID}"
35-
Binding="{Binding Path=(network:SNMPOIDProfileInfo.OID)}"
36-
SortMemberPath="OID"
37-
Width="2*" />
39+
Binding="{Binding Path=(network:SNMPOIDProfileInfo.OID)}"
40+
SortMemberPath="OID"
41+
Width="2*" />
3842
<DataGridTextColumn Header="{x:Static Member=localization:Strings.Mode}"
39-
Binding="{Binding (network:SNMPOIDProfileInfo.Mode)}"
40-
SortMemberPath="Mode"
41-
Width="1*" />
43+
Binding="{Binding (network:SNMPOIDProfileInfo.Mode)}"
44+
SortMemberPath="Mode"
45+
Width="1*" />
4246
</DataGrid.Columns>
4347
<DataGrid.InputBindings>
4448
<KeyBinding Command="{Binding Path=OKCommand}" Key="Return" />
4549
</DataGrid.InputBindings>
4650
<DataGrid.RowStyle>
4751
<Style TargetType="{x:Type TypeName=DataGridRow}"
48-
BasedOn="{StaticResource ResourceKey=MahApps.Styles.DataGridRow}">
52+
BasedOn="{StaticResource ResourceKey=MahApps.Styles.DataGridRow}">
4953
<EventSetter Event="MouseDoubleClick" Handler="DataGridRow_MouseDoubleClick" />
5054
</Style>
5155
</DataGrid.RowStyle>
5256
</DataGrid>
5357
</Grid>
5458
<StackPanel Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Right">
5559
<Button Content="{x:Static Member=localization:Strings.OK}" Command="{Binding Path=OKCommand}"
56-
IsDefault="True" Margin="0,0,10,0">
60+
IsDefault="True" Margin="0,0,10,0">
5761
<Button.Style>
5862
<Style TargetType="{x:Type TypeName=Button}"
59-
BasedOn="{StaticResource ResourceKey=HighlightedButton}">
63+
BasedOn="{StaticResource ResourceKey=HighlightedButton}">
6064
<Setter Property="IsEnabled" Value="True" />
6165
<Style.Triggers>
6266
<MultiDataTrigger>
6367
<MultiDataTrigger.Conditions>
6468
<Condition
65-
Binding="{Binding ElementName=DataGridProfiles, Path=SelectedItems.Count}"
66-
Value="0" />
69+
Binding="{Binding ElementName=DataGridProfiles, Path=SelectedItems.Count}"
70+
Value="0" />
6771
</MultiDataTrigger.Conditions>
6872
<MultiDataTrigger.Setters>
6973
<Setter Property="IsEnabled" Value="False" />
@@ -74,7 +78,7 @@
7478
</Button.Style>
7579
</Button>
7680
<Button Content="{x:Static Member=localization:Strings.Cancel}" Command="{Binding Path=CancelCommand}"
77-
IsCancel="True" Style="{StaticResource ResourceKey=DefaultButton}" />
81+
IsCancel="True" Style="{StaticResource ResourceKey=DefaultButton}" />
7882
</StackPanel>
7983
</Grid>
80-
</UserControl>
84+
</simpleChildWindow:ChildWindow>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using NETworkManager.ViewModels;
2+
using System;
3+
using System.Windows;
4+
using System.Windows.Threading;
5+
6+
namespace NETworkManager.Views;
7+
8+
public partial class SNMPOIDProfilesChildWindow
9+
{
10+
public SNMPOIDProfilesChildWindow(Window parentWindow)
11+
{
12+
InitializeComponent();
13+
14+
// Set the width and height of the child window based on the parent window size
15+
ChildWindowMaxWidth = 850;
16+
ChildWindowMaxHeight = 650;
17+
ChildWindowWidth = parentWindow.ActualWidth * 0.85;
18+
//ChildWindowHeight = parentWindow.ActualHeight * 0.85;
19+
20+
// Update the size of the child window when the parent window is resized
21+
parentWindow.SizeChanged += (_, _) =>
22+
{
23+
ChildWindowWidth = parentWindow.ActualWidth * 0.85;
24+
//ChildWindowHeight = parentWindow.ActualHeight * 0.85;
25+
};
26+
}
27+
28+
private void ChildWindow_OnLoaded(object sender, RoutedEventArgs e)
29+
{
30+
Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(delegate
31+
{
32+
TextBoxSearch.Focus();
33+
}));
34+
}
35+
36+
private void DataGridRow_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
37+
{
38+
var x = (SNMPOIDProfilesViewModel)DataContext;
39+
40+
if (x.OKCommand.CanExecute(null))
41+
x.OKCommand.Execute(null);
42+
}
43+
}

Source/NETworkManager/Views/SNMPOIDProfilesDialog.xaml.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

Source/NETworkManager/Views/SNMPView.xaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
xmlns:viewModels="clr-namespace:NETworkManager.ViewModels"
1212
xmlns:network="clr-namespace:NETworkManager.Models.Network;assembly=NETworkManager.Models"
1313
xmlns:localization="clr-namespace:NETworkManager.Localization.Resources;assembly=NETworkManager.Localization"
14-
xmlns:dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
1514
xmlns:wpfHelpers="clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF"
1615
xmlns:interactivity="http://schemas.microsoft.com/xaml/behaviors"
17-
dialogs:DialogParticipation.Register="{Binding}"
1816
mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:SNMPViewModel}">
1917
<UserControl.Resources>
2018
<converters:BooleanToVisibilityCollapsedConverter x:Key="BooleanToVisibilityCollapsedConverter" />

Source/NETworkManager/Views/SNMPView.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Windows;
55
using System.Windows.Controls;
66
using System.Windows.Data;
7-
using MahApps.Metro.Controls.Dialogs;
87
using NETworkManager.Controls;
98
using NETworkManager.Models.Network;
109
using NETworkManager.Utilities;
@@ -20,7 +19,7 @@ public SNMPView(Guid tabId, SNMPSessionInfo sessionInfo)
2019
{
2120
InitializeComponent();
2221

23-
_viewModel = new SNMPViewModel(DialogCoordinator.Instance, tabId, sessionInfo);
22+
_viewModel = new SNMPViewModel(tabId, sessionInfo);
2423

2524
DataContext = _viewModel;
2625

0 commit comments

Comments
 (0)