Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a MultiSelectionComboBox #4006

Merged
merged 23 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ec0cc46
Implement MultiSelectionComboBox
timunie Dec 15, 2020
5ac9b60
Add MultiSelectionComboBox-Demo
timunie Dec 16, 2020
ba2e160
Imporve selected items appearance
timunie Dec 29, 2020
4558dcc
Add `IParseStringToObject` interface so the user can add missing items
timunie Dec 30, 2020
4fdcac0
Implement ItemContainerStyle and SelectedItemContainerStyle
timunie Jan 1, 2021
e0c32bf
Add `mah:ScrollViewerHelper.IsHorizontalScrollWheelEnabled` to `ListBox`
timunie Jan 1, 2021
74feb22
Better MouseWheel handling
timunie Jan 4, 2021
fa7f1f3
Implement Delay before the Selection gets updated during text input
timunie Jan 6, 2021
b095a82
Formatting and add needed License Text
timunie Jan 6, 2021
722cd11
Handle MouseWheelEvent also if not editable.
timunie Jan 8, 2021
7d125ad
Correct implementation for readonly DependcyProperties
punker76 May 1, 2021
25283e6
Fixing a bug where the text got cleared unexpected
timunie May 4, 2021
9dcdb1e
Add build in IParseStringToObject - Implementation
timunie May 6, 2021
10cb252
Improve adding new items
timunie May 12, 2021
2baf0f8
Fix typo in comment
timunie May 31, 2021
ea2d7d0
Nullable review
punker76 Jul 7, 2021
77692c4
Improve text selection
timunie Jul 8, 2021
d7d6196
Improve ClearTextButton behaviour for MultiSelectionComboBox
timunie Aug 13, 2021
9099c2d
Implement DropDown Header and Footer and Select All Command
timunie Aug 25, 2021
4807b02
Applying XamlStyler
punker76 Oct 21, 2021
693fa2a
Rename BuiltInStringToObjectParser to DefaultStringToObjectParser, ad…
punker76 Oct 22, 2021
273dc03
Add MultiSelectionComboBox to VisualStudioToolsManifest.xml
punker76 Oct 22, 2021
b162aa9
Do some proposed changes
punker76 Oct 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System.Diagnostics;
using System.Windows.Controls;

namespace MetroDemo.ExampleViews
{
/// <summary>
/// Interaction logic for MultiSelectionComboBoxExample.xaml
/// </summary>
public partial class MultiSelectionComboBoxExample : UserControl
{
public MultiSelectionComboBoxExample()
{
this.InitializeComponent();
}

private void Mscb_Example_AddingItem(object? sender, AddingItemEventArgs args)
{
// We don't want to get double entries so let`s check if we already have one.
args.Accepted = args.TargetList is not null && !args.TargetList.Contains(args.ParsedObject);
}

private async void Mscb_Example_AddedItem(object? sender, AddedItemEventArgs args)
{
var window = this.TryFindParent<MetroWindow>();
if (window is null)
{
return;
}

await window.ShowMessageAsync("Added Item", $"Successfully added \"{args.AddedItem}\" to your Items-Collection");
}

private void mscb_Example_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (var item in e.AddedItems)
{
Debug.WriteLine($"MultiSelectionComboBox-Example: Selected item \"{item}\"");
}

foreach (var item in e.RemovedItems)
{
Debug.WriteLine($"MultiSelectionComboBox-Example: Unselected item \"{item}\"");
}
}
}
}
4 changes: 4 additions & 0 deletions src/MahApps.Metro.Samples/MahApps.Metro.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@
<exampleViews:ColorPickerExample />
</TabItem>

<TabItem Header="MultiSelectionComboBox">
<exampleViews:MultiSelectionComboBoxExample />
</TabItem>

<TabItem Header="others">
<ScrollViewer Margin="2"
HorizontalScrollBarVisibility="Auto"
Expand Down
165 changes: 165 additions & 0 deletions src/MahApps.Metro.Samples/MahApps.Metro.Demo/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ public MainWindowViewModel(IDialogCoordinator dialogCoordinator)

this.ToggleSwitchOffCommand = new SimpleCommand<MainWindowViewModel?>(x => x is not null && x.CanUseToggleSwitch,
async x => { await this._dialogCoordinator.ShowMessageAsync(this, "ToggleSwitch", "The ToggleSwitch is now Off."); });

this.MyObjectParser = new ObjectParser(this, this._dialogCoordinator);
}

public ICommand ArtistsDropDownCommand { get; }
Expand Down Expand Up @@ -551,5 +553,168 @@ private void ToggleIconScaling(MultiFrameImageMode? multiFrameImageMode)
public bool IsNoScaleSmallerFrame => ((MetroWindow)Application.Current.MainWindow).IconScalingMode == MultiFrameImageMode.NoScaleSmallerFrame;

public bool IsToggleSwitchVisible { get; set; }

public ObservableCollection<string> Animals { get; } = new()
{
"African elephant",
"Ant",
"Antelope",
"Aphid",
"Arctic wolf",
"Badger",
"Bald eagle",
"Bat",
"Bear",
"Bee",
"Beetle",
"Bengal tiger",
"Bison",
"Butterfly",
"Camel",
"Cat",
"Caterpillar",
"Chicken",
"Chimpanzee",
"Chipmunk",
"Cicada",
"Clam",
"Cockroach",
"Cormorant",
"Cow",
"Coyote",
"Crab",
"Crow",
"Cuckoo",
"Deer",
"Dog",
"Dolphin",
"Donkey",
"Dove",
"Dragonfly",
"Duck",
"Elephant",
"Elk",
"Finch",
"Fish",
"Flamingo",
"Flea",
"Fly",
"Fox",
"Frigatebird",
"Giraffe",
"Goat",
"Goldfish",
"Goose",
"Gorilla",
"Grasshopper",
"Great horned owl",
"Guinea pig",
"Hamster",
"Hare",
"Hawk",
"Hedgehog",
"Hippopotamus",
"Hornbill",
"Horse",
"Horse-fly",
"Howler monkey",
"Hummingbird",
"Hyena",
"Ibis",
"Jackal",
"Jellyfish",
"Kangaroo",
"Koala",
"Ladybugs(NAmE) /ladybirds(BrE)",
"Leopard",
"Lion",
"Lizard",
"Lobster",
"Lynxes",
"Mantis",
"Marten",
"Mole",
"Monkey",
"Mosquito",
"Moth",
"Mouse",
"Octopus",
"Okapi",
"Orangutan",
"Otter",
"Owl",
"Ox",
"Oyster",
"Panda",
"Parrot",
"Pelecaniformes",
"Pelican",
"Penguin",
"Pig",
"Pigeon",
"Porcupine",
"Possum",
"Puma",
"Rabbit",
"Raccoon",
"Rat",
"Raven",
"Red dear",
"Red panda",
"Red squirrel",
"Reindeer",
"Rhinoceros",
"Robin",
"Sandpiper",
"Sea turtle",
"Seahorse",
"Seal",
"Shark",
"Sheep",
"Shell",
"Shrimp",
"Snake",
"Sparrow",
"Squid",
"Squirrel",
"Squirrel monkey",
"Starfish",
"Stork",
"Swallow",
"Swan",
"Termite",
"Tern",
"Tick",
"Tiger",
"Turkey",
"Turtle",
"Walrus",
"Wasp",
"Whale",
"Whitefly",
"Wild boar",
"Wolf",
"Wombat",
"Woodpecker",
"Zebra"
};

public ObservableCollection<string> SelectedAnimals { get; } = new()
{
"Dog",
"Cat",
"Zebra"
};

private object? myFavoriteAnimal;

[Display(Prompt = "Select your favorite animal(s)")]
public object? MyFavoriteAnimal
{
get => this.myFavoriteAnimal;
set => this.Set(ref this.myFavoriteAnimal, value);
}

public ObjectParser? MyObjectParser { get; }
}
}
6 changes: 6 additions & 0 deletions src/MahApps.Metro.Samples/MahApps.Metro.Demo/Models/Album.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public virtual Artist? Artist
get => this._artist;
set => this.Set(ref this._artist, value);
}

public override string ToString()
{
return $"{this.Artist}: {this.Title} ({this.Price.ToString("C")})";
}

}

public static class SampleData
Expand Down
9 changes: 9 additions & 0 deletions src/MahApps.Metro.Samples/MahApps.Metro.Demo/Models/Artist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,14 @@ public List<Album>? Albums
get => this._albums;
set => this.Set(ref this._albums, value);
}

#if NET5_0_OR_GREATER || NETCOREAPP
public override string? ToString()
#else
public override string ToString()
#endif
{
return this.Name ?? base.ToString();
}
}
}
9 changes: 9 additions & 0 deletions src/MahApps.Metro.Samples/MahApps.Metro.Demo/Models/Genre.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,14 @@ public List<Album>? Albums
get => this._albums;
set => this.Set(ref this._albums, value);
}

#if NET5_0_OR_GREATER || NETCOREAPP
public override string? ToString()
#else
public override string ToString()
#endif
{
return this.Name ?? base.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Globalization;

namespace MetroDemo.Models
{
public class ObjectParser : IParseStringToObject
{
private readonly MainWindowViewModel _mainWindowViewModel;
private readonly IDialogCoordinator _dialogCoordinator;

public ObjectParser(MainWindowViewModel mainWindowViewModel, IDialogCoordinator dialogCoordinator)
{
this._mainWindowViewModel = mainWindowViewModel;
this._dialogCoordinator = dialogCoordinator;
}

/// <inheritdoc />
public bool TryCreateObjectFromString(string? input,
out object? result,
CultureInfo? culture = null,
string? stringFormat = null,
Type? elementType = null)
{
if (string.IsNullOrWhiteSpace(input))
{
result = null;
return false;
}

MetroDialogSettings dialogSettings = new MetroDialogSettings
{
AffirmativeButtonText = "Yes",
NegativeButtonText = "No",
DefaultButtonFocus = MessageDialogResult.Affirmative
};

if (this._dialogCoordinator.ShowModalMessageExternal(this._mainWindowViewModel, "Add Animal", $"Do you want to add \"{input}\" to the animals list?", MessageDialogStyle.AffirmativeAndNegative, dialogSettings) == MessageDialogResult.Affirmative)
{
result = input!.Trim();
return true;
}
else
{
result = null;
return false;
}
}
}
}
Loading