Skip to content

Commit

Permalink
Merge pull request #4006 from timunie/feature/MultiSelectionComboBox
Browse files Browse the repository at this point in the history
Implement a MultiSelectionComboBox
  • Loading branch information
punker76 authored Oct 22, 2021
2 parents e33c873 + b162aa9 commit f024a8a
Show file tree
Hide file tree
Showing 30 changed files with 4,401 additions and 7 deletions.

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

0 comments on commit f024a8a

Please sign in to comment.