Skip to content

Instantly share code, notes, and snippets.

@boronology
Created February 17, 2019 00:59
Show Gist options
  • Save boronology/44b52c1e175990f1a8171bca2a475183 to your computer and use it in GitHub Desktop.
Save boronology/44b52c1e175990f1a8171bca2a475183 to your computer and use it in GitHub Desktop.
WPF定番コード集その3。ComboBox
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
namespace TemplateApp.ViewModel
{
public class ComboBoxVM<T> : ViewModelBase
{
private ObservableCollection<ComboBoxSelector<T>> candidates;
public ObservableCollection<ComboBoxSelector<T>> Candidates
{
get { return candidates; }
set { candidates = value; OnPropertyChanged(); }
}
private ComboBoxSelector<T> selectedItem;
public ComboBoxSelector<T> SelectedItem
{
get { return selectedItem; }
set { selectedItem = value; OnPropertyChanged(); }
}
public T SelectedItemValue
{
get { return SelectedItem == null ? default(T) : SelectedItem.Value; }
set { SelectedItem = Candidates.FirstOrDefault(x => x.Value.Equals(value)); }
}
//パラメータから表示用の文字列に変換する関数
public delegate string nameConverter(T value);
public ComboBoxVM(IEnumerable<T> ts, T defaultValue)
: this(ts.Select(x => new ComboBoxSelector<T>(x.ToString(), x)))
{
SelectedItemValue = defaultValue;
}
public ComboBoxVM(IEnumerable<T> ts)
: this(ts.Select(x => new ComboBoxSelector<T>(x.ToString(), x)))
{
SelectedItem = Candidates.FirstOrDefault();
}
public ComboBoxVM(IEnumerable<T> ts, nameConverter func, T defaultValue)
: this(ts.Select(x => new ComboBoxSelector<T>(func(x), x)))
{
SelectedItemValue = defaultValue;
}
public ComboBoxVM(IEnumerable<T> ts, nameConverter func)
: this(ts.Select(x => new ComboBoxSelector<T>(func(x), x)))
{
SelectedItem = Candidates.FirstOrDefault();
}
protected ComboBoxVM(IEnumerable<ComboBoxSelector<T>> selectors)
{
Candidates = new ObservableCollection<ComboBoxSelector<T>>(selectors);
}
}
public class ComboBoxEnumVM<T> : ComboBoxVM<T> where T : struct
{
public ComboBoxEnumVM(T defaultValue)
: base(Enum.GetValues(typeof(T)).OfType<T>().Select(x => new ComboBoxSelector<T>(x.ToString(), x)))
{
SelectedItemValue = defaultValue;
}
public ComboBoxEnumVM()
: base(Enum.GetValues(typeof(T)).OfType<T>().Select(x => new ComboBoxSelector<T>(x.ToString(), x)))
{
SelectedItem = Candidates.FirstOrDefault();
}
public ComboBoxEnumVM(nameConverter func, T defaultValue)
: base(Enum.GetValues(typeof(T)).OfType<T>().Select(x => new ComboBoxSelector<T>(func(x), x)))
{
SelectedItemValue = defaultValue;
}
public ComboBoxEnumVM(nameConverter func)
: base(Enum.GetValues(typeof(T)).OfType<T>().Select(x => new ComboBoxSelector<T>(func(x), x)))
{
SelectedItem = Candidates.FirstOrDefault();
}
}
public class ComboBoxSelector<T>
{
public string Label { get; private set; }
public T Value { get; private set; }
public ComboBoxSelector(string label, T value)
{
Label = label;
Value = value;
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TemplateApp">
<!--ComboBoxVM/ComboBoxEnumVMをDataContextとするComboBoxのStyle-->
<Style x:Key="ComboBoxStyleBase" TargetType="{x:Type ComboBox}">
<Style.Setters>
<Setter Property="ItemsSource" Value="{Binding Candidates}"/>
<Setter Property="SelectedItem" Value="{Binding SelectedItem}"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Label Content="{Binding Label}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</ResourceDictionary>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment