Created
September 27, 2023 17:26
-
-
Save mkeneqa/5276870c16984b6fd121d714a8c2a637 to your computer and use it in GitHub Desktop.
Resuable Dropdown/ComboBox component for (Calibrun) MVVM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Linq; | |
using Caliburn.Micro; | |
using Scheduler.Data; | |
namespace MyApp | |
{ | |
public class DropDownViewModel : Screen | |
{ | |
private int _selectedIndex; | |
private int _selectedValue; | |
private IEnumerable<DropDownItem> _dropDownList; | |
private readonly ObservableCollection<DropDownItem> _ddItems = new ObservableCollection<DropDownItem>(); | |
public IEnumerable<DropDownItem> DropDownList | |
{ | |
get => _dropDownList; | |
set | |
{ | |
_dropDownList = value; | |
loadDropDownList(); | |
} | |
} | |
public int Counter { get; set; } = 0; | |
public void AddItem(string itemName, int? itemValue=null) | |
{ | |
var _itemValue = itemValue ?? Counter; | |
_ddItems.Add(new DropDownItem | |
{ | |
DisplayName = itemName, | |
Value = _itemValue, | |
Index = Counter++ | |
}); | |
Items = _ddItems; | |
} | |
private void loadDropDownList() | |
{ | |
foreach (var dropDownItem in DropDownList) | |
{ | |
_ddItems.Add(new DropDownItem | |
{ | |
DisplayName = dropDownItem.DisplayName, | |
Value = dropDownItem.Value, | |
Index = Counter++ | |
}); | |
} | |
Items = _ddItems; | |
} | |
public void UpdateSelectedItem(int selectedItem, bool byIndex = true) | |
{ | |
var dd = byIndex | |
? Items.FirstOrDefault(x => x.Index == selectedItem) | |
: Items.FirstOrDefault(x => x.Value == selectedItem); | |
if (dd?.Index != null) | |
{ | |
_selectedIndex = dd.Index; | |
_selectedValue = dd.Value; | |
SelectedDisplayName = dd.DisplayName; | |
NotifyOfPropertyChange(() => SelectedIndex); | |
NotifyOfPropertyChange(() => SelectedValue); | |
NotifyOfPropertyChange(() => SelectedDisplayName); | |
} | |
} | |
public int SelectedValue | |
{ | |
get => _selectedValue; | |
set => UpdateSelectedItem(value, byIndex : false); | |
} | |
public int SelectedIndex | |
{ | |
get => _selectedIndex; | |
set => UpdateSelectedItem(value, byIndex : true); | |
} | |
public ObservableCollection<DropDownItem> Items { get; set; } | |
public string SelectedDisplayName { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
..... | |
<Label Grid.Row="1" Grid.Column="0">My Options List</Label> | |
<ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding MyDropDown.Items}" | |
SelectedValuePath="Value" DisplayMemberPath="DisplayName" | |
SelectedValue="{Binding SelectedValue}" | |
IsEditable="False" HorizontalAlignment="Stretch" MaxDropDownHeight="600" | |
/> | |
..... | |
<Label Grid.Row="2" Grid.Column="0">My Options List 2</Label> | |
<ComboBox Grid.Row="2" Grid.Column="1" ItemsSource="{Binding MyDropDown2.Items}" | |
SelectedValuePath="Value" DisplayMemberPath="DisplayName" | |
SelectedValue="{Binding SelectedValue}" | |
IsEditable="False" HorizontalAlignment="Stretch" MaxDropDownHeight="600" | |
/> | |
.... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyApp | |
{ | |
public abstract class TestProjectViewModel : Screen | |
{ | |
public TestProjectViewModel() | |
{ | |
MyDropDown = new DropDownViewModel() | |
{ | |
DropDownList = DropDownHelper.GetMyOptionsList(), | |
SelectedValue = MySelectedItem > 0 ? MySelectedItem : 0 | |
}; | |
MyDropDown2 = new DropDownViewModel(); | |
MyDropDown2.AddItem(""); | |
MyDropDown2.AddItem("Item 1"); | |
MyDropDown2.AddItem("Item 2"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment