Last active
December 2, 2017 07:52
-
-
Save yoshiiz/b4b3bec5f048d5d9fdcda74e4db253ef to your computer and use it in GitHub Desktop.
[Visual C# 2017][WPF] WpfListBoxDoubleClickSample
This file contains 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; | |
using System.Windows.Input; | |
namespace WpfListBoxDoubleClickSample.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; } | |
} | |
} | |
} |
This file contains 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
<Window x:Class="WpfListBoxDoubleClickSample.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:WpfListBoxDoubleClickSample" | |
mc:Ignorable="d" | |
xmlns:vm="clr-namespace:WpfListBoxDoubleClickSample.ViewModels" | |
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" | |
Title="MainWindow" Height="350" Width="525"> | |
<Window.DataContext> | |
<vm:MainViewModel /> | |
</Window.DataContext> | |
<Grid> | |
<ListBox ItemsSource="{Binding Path=Items}" | |
SelectedItem="{Binding Path=SelectedItem}"> | |
<ListBox.ItemTemplate> | |
<DataTemplate> | |
<TextBlock> | |
<TextBlock.Text> | |
<MultiBinding StringFormat="{}{0},{1}"> | |
<Binding Path="Name" /> | |
<Binding Path="Age" /> | |
</MultiBinding> | |
</TextBlock.Text> | |
</TextBlock> | |
</DataTemplate> | |
</ListBox.ItemTemplate> | |
<i:Interaction.Triggers> | |
<i:EventTrigger EventName="MouseDoubleClick"> | |
<i:InvokeCommandAction Command="{ | |
Binding Path=ShowSelectedItemCommand, Mode=OneWay}" /> | |
</i:EventTrigger> | |
</i:Interaction.Triggers> | |
</ListBox> | |
</Grid> | |
</Window> |
This file contains 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.Windows; | |
namespace WpfListBoxDoubleClickSample | |
{ | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
} | |
} |
This file contains 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 WpfListBoxDoubleClickSample.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; | |
} | |
} | |
} |
This file contains 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.Windows; | |
using System.ComponentModel; // INotifyPropertyChanged | |
using System.Collections.ObjectModel; // ObservableCollection | |
using WpfListBoxDoubleClickSample.Commands; | |
using WpfListBoxDoubleClickSample.Models; | |
namespace WpfListBoxDoubleClickSample.ViewModels | |
{ | |
public class MainViewModel : INotifyPropertyChanged | |
{ | |
// --------------------------------------------------------------------- | |
// プロパティ - リスト | |
private ObservableCollection<Person> _items; | |
public ObservableCollection<Person> Items | |
{ | |
get { return _items; } | |
set | |
{ | |
_items = value; | |
this.OnPropertyChanged(nameof(Items)); | |
} | |
} | |
public Person SelectedItem { get; set; } | |
// --------------------------------------------------------------------- | |
// プロパティ - コマンド | |
private DelegateCommand _showSelectedItemCommand; | |
public DelegateCommand ShowSelectedItemCommand | |
{ | |
get | |
{ | |
if (_showSelectedItemCommand == null) | |
{ | |
_showSelectedItemCommand = new DelegateCommand( | |
() => this.DoShowSelectedItem(), | |
() => this.SelectedItem != null | |
); | |
} | |
return _showSelectedItemCommand; | |
} | |
} | |
private void DoShowSelectedItem() | |
{ | |
string message = string.Format("{0},{1}", | |
this.SelectedItem.Name, this.SelectedItem.Age); | |
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