Last active
January 3, 2018 21:56
-
-
Save yoshiiz/a5357b1cb37a5e392a404b85d5ad997b to your computer and use it in GitHub Desktop.
[Visual C# 2017][WPF] WpfDoubleClickAttachedBehaviorSample
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; | |
using System.Windows.Controls; | |
using System.Windows.Input; | |
namespace WpfDoubleClickAttachedBehaviorSample.Behaviors | |
{ | |
public class DoubleClickAttachedBehavior | |
{ | |
public static readonly DependencyProperty CommandProperty = | |
DependencyProperty.RegisterAttached( | |
"Command", typeof(ICommand), | |
typeof(DoubleClickAttachedBehavior), | |
new FrameworkPropertyMetadata | |
{ | |
DefaultValue = null, | |
PropertyChangedCallback = CommandPropertyChangedCallBack | |
}); | |
public static ICommand GetCommand(DependencyObject obj) | |
{ | |
return (ICommand)obj.GetValue(CommandProperty); | |
} | |
public static void SetCommand(DependencyObject obj, object value) | |
{ | |
obj.SetValue(CommandProperty, value); | |
} | |
private static void CommandPropertyChangedCallBack( | |
DependencyObject obj, DependencyPropertyChangedEventArgs e) | |
{ | |
var control = obj as Control; | |
if (control == null) { return; } | |
if (e.OldValue != null) | |
{ | |
control.MouseDoubleClick -= Control_MouseDoubleClick; | |
} | |
if (e.NewValue != null) | |
{ | |
control.MouseDoubleClick += Control_MouseDoubleClick; | |
} | |
} | |
private static void Control_MouseDoubleClick( | |
object sender, MouseButtonEventArgs e) | |
{ | |
var control = sender as Control; | |
if (control == null) { return; } | |
var command = GetCommand(control); | |
if (command == null) { return; } | |
var args = new Tuple<Control, MouseButtonEventArgs>(control, e); | |
if (command.CanExecute(args)) | |
{ | |
command.Execute(args); | |
} | |
} | |
} | |
} |
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 WpfDoubleClickAttachedBehaviorSample.Commands | |
{ | |
public class DelegateCommand<T> : ICommand | |
{ | |
private Action<T> _execute; | |
private Func<T, bool> _canExecute; | |
public DelegateCommand(Action<T> execute, Func<T, bool> canExecute) | |
{ | |
_execute = execute; | |
_canExecute = canExecute; | |
} | |
public void Execute(object parameter) | |
{ | |
_execute((T)parameter); | |
} | |
public bool CanExecute(object parameter) | |
{ | |
return _canExecute((T)parameter); | |
} | |
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="WpfDoubleClickAttachedBehaviorSample.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:WpfDoubleClickAttachedBehaviorSample" | |
mc:Ignorable="d" | |
xmlns:vm="clr-namespace:WpfDoubleClickAttachedBehaviorSample.ViewModels" | |
xmlns:bhv="clr-namespace:WpfDoubleClickAttachedBehaviorSample.Behaviors" | |
Title="MainWindow" Height="350" Width="525"> | |
<Window.DataContext> | |
<vm:MainViewModel /> | |
</Window.DataContext> | |
<Grid> | |
<ListBox ItemsSource="{Binding Path=Items}" | |
SelectedItem="{Binding Path=SelectedItem}" | |
bhv:DoubleClickAttachedBehavior.Command= | |
"{Binding Path=ListBoxMouseDoubleClickCommand}"> | |
<ListBox.ItemTemplate> | |
<DataTemplate> | |
<TextBlock> | |
<TextBlock.Text> | |
<MultiBinding StringFormat="{}{0},{1}"> | |
<Binding Path="Name" /> | |
<Binding Path="Age" /> | |
</MultiBinding> | |
</TextBlock.Text> | |
</TextBlock> | |
</DataTemplate> | |
</ListBox.ItemTemplate> | |
</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 WpfDoubleClickAttachedBehaviorSample | |
{ | |
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 WpfDoubleClickAttachedBehaviorSample.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; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Input; | |
using System.ComponentModel; // INotifyPropertyChanged | |
using System.Collections.ObjectModel; // ObservableCollection | |
using WpfDoubleClickAttachedBehaviorSample.Commands; | |
using WpfDoubleClickAttachedBehaviorSample.Models; | |
namespace WpfDoubleClickAttachedBehaviorSample.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<Tuple<Control, MouseButtonEventArgs>> | |
_listBoxMouseDoubleClickCommand; | |
public DelegateCommand<Tuple<Control, MouseButtonEventArgs>> | |
ListBoxMouseDoubleClickCommand | |
{ | |
get | |
{ | |
if (_listBoxMouseDoubleClickCommand == null) | |
{ | |
_listBoxMouseDoubleClickCommand = new DelegateCommand | |
<Tuple<Control, MouseButtonEventArgs>>( | |
(args) => this.ListBoxMouseDoubleClick( | |
args.Item1, args.Item2), | |
(args) => true | |
); | |
} | |
return _listBoxMouseDoubleClickCommand; | |
} | |
} | |
private void ListBoxMouseDoubleClick( | |
Control control, MouseButtonEventArgs e) | |
{ | |
var listBox = control as ListBox; | |
if (listBox == null) { return; } | |
if (e == null) { return; } | |
this.ShowSelectedItem(); | |
} | |
private void ShowSelectedItem() | |
{ | |
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