From 5814c94cd4a6f4d6405136817a810e16e6854bbc Mon Sep 17 00:00:00 2001 From: "Frank R. Haugen" Date: Mon, 12 Aug 2024 20:23:57 +0200 Subject: [PATCH] Add DropdownWindow and standardize window properties Introduce a new `DropdownWindow` class that uses a custom dropdown control, and standardize the width, height, and startup location properties for all windows. Adjustments in the `Dropdown` control improve type safety and data handling. --- Frank.Wpf.Controls.SimpleInputs/Dropdown.cs | 20 +++--- Frank.Wpf.Tests.App/DefaultWindow.cs | 63 ++++++++++++++++--- Frank.Wpf.Tests.App/Windows/DropdownWindow.cs | 40 ++++++++++++ 3 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 Frank.Wpf.Tests.App/Windows/DropdownWindow.cs diff --git a/Frank.Wpf.Controls.SimpleInputs/Dropdown.cs b/Frank.Wpf.Controls.SimpleInputs/Dropdown.cs index f1941f8..3865dba 100644 --- a/Frank.Wpf.Controls.SimpleInputs/Dropdown.cs +++ b/Frank.Wpf.Controls.SimpleInputs/Dropdown.cs @@ -1,7 +1,5 @@ -using System.Diagnostics; -using System.Windows; +using System.Windows; using System.Windows.Controls; -using Frank.Wpf.Core; namespace Frank.Wpf.Controls.SimpleInputs; @@ -23,18 +21,14 @@ public required IEnumerable Items public required Func DisplayFunc { - init - { - _comboBox.DisplayMemberPath = nameof(ComboBoxItem.Content); - _comboBox.ItemTemplate = CreateDataTemplate(value); - } + init => _comboBox.ItemTemplate = CreateDataTemplate(value); } public required Action SelectionChangedAction { get; init; } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { - if (_comboBox.SelectedItem is T selectedItem && SelectionChangedAction != null) + if (_comboBox.SelectedItem is T selectedItem) { SelectionChangedAction(selectedItem); } @@ -63,14 +57,14 @@ public FuncValueConverter(Func func) _func = func; } - public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + public object? Convert(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture) { return value is TInput input ? _func(input) : default; } - - public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + + public object? ConvertBack(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture) { - throw new NotImplementedException(); + return value is TOutput output ? output : default; } } } \ No newline at end of file diff --git a/Frank.Wpf.Tests.App/DefaultWindow.cs b/Frank.Wpf.Tests.App/DefaultWindow.cs index 8474cbc..442b08c 100644 --- a/Frank.Wpf.Tests.App/DefaultWindow.cs +++ b/Frank.Wpf.Tests.App/DefaultWindow.cs @@ -1,4 +1,5 @@ -using System.Windows.Controls; +using System.Windows; +using System.Windows.Controls; using Frank.Wpf.Controls.SimpleInputs; using Frank.Wpf.Hosting; using Frank.Wpf.Tests.App.Windows; @@ -11,42 +12,90 @@ public class DefaultWindow : MainWindow public DefaultWindow() { + var defaultWidth = 400; + var defaultHeight = 300; + var defaultPosition = WindowStartupLocation.CenterScreen; + Title = "Frank.Wpf.Tests.App"; Content = _windowButtons; + Width = defaultWidth; + Height = defaultHeight; + WindowStartupLocation = defaultPosition; _windowButtons.AddButton("Show SQL Runner", (sender, args) => { - var window = new SqlLiteWindow(); + var window = new SqlLiteWindow + { + Width = defaultWidth, + Height = defaultHeight, + WindowStartupLocation = defaultPosition + }; window.Show(); }); _windowButtons.AddButton("Show Json Window", (sender, args) => { - var window = new JsonWindow(); + var window = new JsonWindow + { + Width = defaultWidth, + Height = defaultHeight, + WindowStartupLocation = defaultPosition + }; window.Show(); }); _windowButtons.AddButton("Show Console Window", (sender, args) => { - var window = new ConsoleWindow(); + var window = new ConsoleWindow + { + Width = defaultWidth, + Height = defaultHeight, + WindowStartupLocation = defaultPosition + }; window.Show(); }); _windowButtons.AddButton("Show Custom List Box Window", (sender, args) => { - var window = new CustomListBoxWindow(); + var window = new CustomListBoxWindow + { + Width = defaultWidth, + Height = defaultHeight, + WindowStartupLocation = defaultPosition + }; window.Show(); }); _windowButtons.AddButton("Show Searchable Selection List Window", (sender, args) => { - var window = new SearchableSelectionListWindow(); + var window = new SearchableSelectionListWindow + { + Width = defaultWidth, + Height = defaultHeight, + WindowStartupLocation = defaultPosition + }; window.Show(); }); _windowButtons.AddButton("Show Key-Value Pair Editor Window", (sender, args) => { - var window = new KvpEditorWindow(); + var window = new KvpEditorWindow + { + Width = defaultWidth, + Height = defaultHeight, + WindowStartupLocation = defaultPosition + }; + window.Show(); + }); + + _windowButtons.AddButton("Show Dropdown Window", (sender, args) => + { + var window = new DropdownWindow + { + Width = defaultWidth, + Height = defaultHeight, + WindowStartupLocation = defaultPosition + }; window.Show(); }); } diff --git a/Frank.Wpf.Tests.App/Windows/DropdownWindow.cs b/Frank.Wpf.Tests.App/Windows/DropdownWindow.cs new file mode 100644 index 0000000..a0c0459 --- /dev/null +++ b/Frank.Wpf.Tests.App/Windows/DropdownWindow.cs @@ -0,0 +1,40 @@ +using System.Windows; +using System.Windows.Controls; +using Frank.Wpf.Controls.SimpleInputs; + +namespace Frank.Wpf.Tests.App.Windows; + +public class DropdownWindow : Window +{ + + private readonly Dropdown _dropdown; + + public DropdownWindow() + { + _dropdown = new Dropdown() + { + Items = new List + { + new MyClass { Id = Guid.NewGuid(), Name = "Frank", Age = 30 }, + new MyClass { Id = Guid.NewGuid(), Name = "John", Age = 40 }, + new MyClass { Id = Guid.NewGuid(), Name = "Jane", Age = 50 } + }, + DisplayFunc = x => x.Name, + SelectionChangedAction = x => { MessageBox.Show($"Selected {x.Name} with Id {x.Id} and Age {x.Age}"); } + }; + + var stackPanel = new StackPanel(); + stackPanel.Children.Add(_dropdown); + + Content = stackPanel; + } + + private class MyClass + { + public Guid Id { get; set; } + + public string Name { get; set; } + + public int Age { get; set; } + } +} \ No newline at end of file