Ejemplo n.º 1
0
        protected override void OnAttach()
        {
            this.AssociatedObject.ItemContainerStyle = new Func <Style>(() =>
            {
                var style = new Style(typeof(ListViewItem))
                {
                    BasedOn = (Style)Application.Current.FindResource(typeof(ListViewItem))
                };

                var mouseDoubleClickEventTrigger = new EventSetter(ListViewItem.MouseDoubleClickEvent,
                                                                   new MouseButtonEventHandler((s, e) =>
                {
                    var sender  = (s as ListViewItem).DataContext;
                    var command = ListViewProperties.GetCommand(this.AssociatedObject);
                    if (command != null && command.CanExecute(sender))
                    {
                        command.Execute(sender);
                    }

                    (e as MouseButtonEventArgs).Handled = true;
                }));

                var mouseKeyUpEventTrigger = new EventSetter(ListViewItem.KeyDownEvent,
                                                             new KeyEventHandler((s, e) =>
                {
                    var args = e as KeyEventArgs;

                    if (args.Key == Key.Enter && args.SystemKey == Key.None)
                    {
                        var sender  = (s as ListViewItem).DataContext;
                        var command = ListViewProperties.GetCommand(this.AssociatedObject);
                        if (command != null && command.CanExecute(sender))
                        {
                            command.Execute(sender);
                        }

                        args.Handled = true;
                    }
                }));

                style.Setters.Add(mouseDoubleClickEventTrigger);
                style.Setters.Add(mouseKeyUpEventTrigger);

                return(style);
            })();
            this.AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
            this.SetBinding(ListViewBehaviour.ViewProperty, this.AssociatedObject, new PropertyPath("View"), BindingMode.OneWay);
        }
Ejemplo n.º 2
0
        private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedItems = this.AssociatedObject.SelectedItems as IEnumerable;

            ListViewProperties.SetSelectedItems(this.AssociatedObject, selectedItems);
        }