Skip to content

Commit

Permalink
Add UI enhancements and selection functionality to DropdownWindow
Browse files Browse the repository at this point in the history
Added Title, dimensions, and startup location to DropdownWindow. Introduced "Show Selected" and "Set Selection" buttons for better UI interaction. Modified Dropdown class to include SetSelectedItem method and improve item selection handling.
  • Loading branch information
frankhaugen committed Aug 15, 2024
1 parent 9e1786d commit 8012c8b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Frank.Wpf.Controls.SimpleInputs/Dropdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public required Func<T, string> DisplayFunc
}

public required Action<T> SelectionChangedAction { get; init; }

public void SetSelectedItem(T item)
{
// Ensure the item is in the list
if (!Items.Contains(item))
throw new ArgumentException("Item is not in the list of items");

_comboBox.SelectedItem = item;
}

public T? SelectedItem => _comboBox.SelectedItem is T item ? item : default;

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Expand Down
46 changes: 44 additions & 2 deletions Frank.Wpf.Tests.App/Windows/DropdownWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ namespace Frank.Wpf.Tests.App.Windows;
public class DropdownWindow : Window
{

private readonly Dropdown<MyClass> _dropdown;
private readonly Dropdown<MyClass?> _dropdown;

public DropdownWindow()
{
_dropdown = new Dropdown<MyClass>()
Title = "Dropdown Window";
Width = 400;
Height = 300;
WindowStartupLocation = WindowStartupLocation.CenterScreen;

_dropdown = new Dropdown<MyClass?>()
{
Items = new List<MyClass>
{
Expand All @@ -22,9 +27,46 @@ public DropdownWindow()
DisplayFunc = x => x.Name,
SelectionChangedAction = x => { MessageBox.Show($"Selected {x.Name} with Id {x.Id} and Age {x.Age}"); }
};
var showSelectedButton = new Button()
{
Content = "Show Selected",
Margin = new Thickness(0, 10, 0, 0)
};
showSelectedButton.Click += (sender, args) =>
{
var selected = _dropdown.SelectedItem;
MessageBox.Show($"Selected {selected.Name} with Id {selected.Id} and Age {selected.Age}");
};

var setSelectionButton = new Button()
{
Content = "Set Selection",
Margin = new Thickness(0, 10, 0, 0)
};
setSelectionButton.Click += (sender, args) =>
{
var random = new Random();
var randomIndex = random.Next(0, _dropdown.Items.Count());
MyClass? randomItem = null;
while (randomItem == null)
{
var item = _dropdown.Items.ElementAt(randomIndex);
if (item != _dropdown.SelectedItem)
{
randomItem = item;
}
else
{
randomIndex = random.Next(0, _dropdown.Items.Count());
}
}
_dropdown.SetSelectedItem(randomItem);
};

var stackPanel = new StackPanel();
stackPanel.Children.Add(_dropdown);
stackPanel.Children.Add(showSelectedButton);
stackPanel.Children.Add(setSelectionButton);

Content = stackPanel;
}
Expand Down

0 comments on commit 8012c8b

Please sign in to comment.