Skip to content

Instantly share code, notes, and snippets.

@yoshiiz
Last active January 3, 2018 21:55
Show Gist options
  • Save yoshiiz/ea576f97e973fa300b07ac70c10a3792 to your computer and use it in GitHub Desktop.
Save yoshiiz/ea576f97e973fa300b07ac70c10a3792 to your computer and use it in GitHub Desktop.
[Visual C# 2017][WPF] WpfBindableListBoxSample
using System;
using System.Windows.Input;
namespace WpfBindableListBoxSample.Commands
{
public class DelegateCommand : ICommand
{
private Action _execute;
private Func<bool> _canExecute;
public DelegateCommand(Action execute, Func<bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public void Execute(object parameter)
{
_execute();
}
public bool CanExecute(object parameter)
{
return _canExecute();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}
using System.Windows;
using System.Windows.Controls;
using System.Collections; // IList
namespace WpfBindableListBoxSample.Controls
{
public class BindableListBox : ListBox
{
public new static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register(
nameof(SelectedItems), typeof(IList),
typeof(BindableListBox),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public new IList SelectedItems
{
get { return (IList)this.GetValue(SelectedItemsProperty); }
set { this.SetValue(SelectedItemsProperty, value); }
}
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
base.OnSelectionChanged(e);
this.SelectedItems = base.SelectedItems;
}
}
}
<Window x:Class="WpfBindableListBoxSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfBindableListBoxSample"
mc:Ignorable="d"
xmlns:vm="clr-namespace:WpfBindableListBoxSample.ViewModels"
xmlns:ctrl="clr-namespace:WpfBindableListBoxSample.Controls"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Gesture="Enter"
Command="{Binding Path=ShowSelectedItemsCommand}" />
</Window.InputBindings>
<Grid>
<ctrl:BindableListBox SelectionMode="Extended"
ItemsSource="{Binding Path=Items}"
SelectedItems="{Binding SelectedItems}">
<ctrl:BindableListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0},{1}">
<Binding Path="Name" />
<Binding Path="Age" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ctrl:BindableListBox.ItemTemplate>
</ctrl:BindableListBox>
</Grid>
</Window>
using System.Windows;
namespace WpfBindableListBoxSample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
namespace WpfBindableListBoxSample.Models
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
}
using System;
using System.Windows;
using System.ComponentModel; // INotifyPropertyChanged
using System.Collections.ObjectModel; // ObservableCollection
using System.Collections; // IList
using WpfBindableListBoxSample.Commands;
using WpfBindableListBoxSample.Models;
namespace WpfBindableListBoxSample.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
// ---------------------------------------------------------------------
// プロパティ - リスト
private ObservableCollection<Person> _items;
public ObservableCollection<Person> Items
{
get { return _items; }
set
{
_items = value;
this.OnPropertyChanged(nameof(Items));
}
}
public IList SelectedItems { get; set; }
// ---------------------------------------------------------------------
// プロパティ - コマンド
private DelegateCommand _showSelectedItemsCommand;
public DelegateCommand ShowSelectedItemsCommand
{
get
{
if (_showSelectedItemsCommand == null)
{
_showSelectedItemsCommand = new DelegateCommand(
() => this.DoShowSelectedItems(),
() => this.SelectedItems != null
);
}
return _showSelectedItemsCommand;
}
}
private void DoShowSelectedItems()
{
string message = "";
foreach (Person item in this.SelectedItems)
{
message += item.Name + "," + item.Age + Environment.NewLine;
}
MessageBox.Show(message);
}
// ---------------------------------------------------------------------
// コンストラクタ
public MainViewModel()
{
// 動作テスト用データ
this.Items = new ObservableCollection<Person>();
this.Items.Add(new Person("太郎", 39));
this.Items.Add(new Person("花子", 34));
this.Items.Add(new Person("次郎", 28));
this.Items.Add(new Person("良子", 22));
this.Items.Add(new Person("三郎", 15));
}
// ---------------------------------------------------------------------
// インターフェースの実装 - INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment