예제 #1
0
        static ItemsElementBehavior()
        {
            BindableSelectedItemsProperty = DependencyProperty.Register(
                "BindableSelectedItems", typeof(IList), typeof(System.Windows.Controls.ListBox));

            HasBindableSelectedItemsProperty = DependencyProperty.RegisterAttached(
                "HasBindableSelectedItems", typeof(bool), typeof(System.Windows.Controls.ListBox), new PropertyMetadata(false));

            SelectionChangedHandlerProperty = DependencyProperty.RegisterAttached(
                "SelectionChangedHandler", typeof(SelectionChangedHandler), typeof(System.Windows.Controls.ListBox));

            DefaultSelectedIndexProperty = DependencyProperty.RegisterAttached(
                "DefaultSelectedIndex", typeof(int), typeof(System.Windows.Controls.ListBox), new UIPropertyMetadata(0));

            ForceSingleSelectionModeItemIndexProperty = DependencyProperty.RegisterAttached(
                "ForceSingleSelectionModeItemIndex", typeof(int), typeof(System.Windows.Controls.ListBox), new UIPropertyMetadata(0));

            SortInfoPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
                "SortInfo", typeof(SortInfo), typeof(ItemsElementBehavior), new PropertyMetadata());

            MultiUpdatePropertyNamesProperty = DependencyProperty.RegisterAttached(
                "MultiUpdatePropertyNames", typeof(IEnumerable), typeof(ItemsElementBehavior), new UIPropertyMetadata(null, (obj, args) =>
            {
                var listBox = (System.Windows.Controls.ListBox)obj;
                if (args.NewValue != null)
                {
                    new ListBoxMultiUpdatePropertyNamesBehavior(listBox);
                }
            }));

            EnableSortingProperty = DependencyProperty.RegisterAttached(
                "EnableSorting", typeof(bool), typeof(ListView), new UIPropertyMetadata(false, (obj, args) =>
            {
                if (args.NewValue != null && args.NewValue is bool && ((bool)args.NewValue) == true)
                {
                    var listView = (ListView)obj;

                    listView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler((sender, e) =>
                    {
                        var header = e.OriginalSource as GridViewColumnHeader;
                        if (header != null && header.Column != null && header.Column.DisplayMemberBinding != null)
                        {
                            string bindingProperty          = (header.Column.DisplayMemberBinding as Binding).Path.Path;
                            ListSortDirection sortDirection = ListSortDirection.Ascending;
                            if (listView.Items.SortDescriptions.Count > 0)
                            {
                                SortDescription sd = listView.Items.SortDescriptions[0];
                                sortDirection      = (ListSortDirection)(1 - (int)sd.Direction);
                                listView.Items.SortDescriptions.Clear();
                            }

                            SortInfo sortInfo = listView.GetValue(SortInfoPropertyKey.DependencyProperty) as SortInfo;
                            if (sortInfo == null)
                            {
                                sortInfo = new SortInfo()
                                {
                                    LastSortColumn = header, CurrentAdorner = new ArrowAdorner(header, new ListSortDecorator {
                                        SortDirection = sortDirection
                                    })
                                };
                                AdornerLayer.GetAdornerLayer(header).Add(sortInfo.CurrentAdorner);
                                listView.SetValue(SortInfoPropertyKey, sortInfo);
                            }
                            else
                            {
                                if (sortInfo.LastSortColumn != header)
                                {
                                    AdornerLayer.GetAdornerLayer(sortInfo.LastSortColumn).Remove(sortInfo.CurrentAdorner);
                                    sortInfo.LastSortColumn = header;
                                    sortInfo.CurrentAdorner = new ArrowAdorner(header, new ListSortDecorator {
                                        SortDirection = sortDirection
                                    });
                                    AdornerLayer.GetAdornerLayer(header).Add(sortInfo.CurrentAdorner);
                                }
                                sortInfo.CurrentAdorner.Child.SortDirection = sortDirection;
                            }
                            try
                            {
                                listView.Items.SortDescriptions.Add(new SortDescription(bindingProperty, sortDirection));
                            }
                            catch (InvalidOperationException)
                            {
#if DEBUG
                                throw;
#endif
                            }
                        }
                    }));
                }
            }));
        }
예제 #2
0
        static ItemsElementBehavior()
        {
            BindableSelectedItemsProperty = DependencyProperty.Register(
                "BindableSelectedItems", typeof(IList), typeof(System.Windows.Controls.ListBox));

            HasBindableSelectedItemsProperty = DependencyProperty.RegisterAttached(
                "HasBindableSelectedItems", typeof(bool), typeof(System.Windows.Controls.ListBox), new PropertyMetadata(false));

            SelectionChangedHandlerProperty = DependencyProperty.RegisterAttached(
                "SelectionChangedHandler", typeof(SelectionChangedHandler), typeof(System.Windows.Controls.ListBox));

            DefaultSelectedIndexProperty = DependencyProperty.RegisterAttached(
                "DefaultSelectedIndex", typeof(int), typeof(System.Windows.Controls.ListBox), new UIPropertyMetadata(0));

            ForceSingleSelectionModeItemIndexProperty = DependencyProperty.RegisterAttached(
                "ForceSingleSelectionModeItemIndex", typeof(int), typeof(System.Windows.Controls.ListBox), new UIPropertyMetadata(0));

            SortInfoPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
                "SortInfo", typeof(SortInfo), typeof(ItemsElementBehavior), new PropertyMetadata());

            MultiUpdatePropertyNamesProperty = DependencyProperty.RegisterAttached(
                "MultiUpdatePropertyNames", typeof(IEnumerable), typeof(ItemsElementBehavior), new UIPropertyMetadata(null, (obj, args) =>
                {
                    var listBox = (System.Windows.Controls.ListBox)obj;
                    if (args.NewValue != null)
                        new ListBoxMultiUpdatePropertyNamesBehavior(listBox);
                }));

            EnableSortingProperty = DependencyProperty.RegisterAttached(
                "EnableSorting", typeof(bool), typeof(ListView), new UIPropertyMetadata(false, (obj, args) =>
                {
                    if (args.NewValue != null && args.NewValue is bool && ((bool)args.NewValue) == true)
                    {
                        var listView = (ListView)obj;

                        listView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler((sender, e) =>
                        {
                            var header = e.OriginalSource as GridViewColumnHeader;
                            if (header != null && header.Column != null && header.Column.DisplayMemberBinding != null)
                            {
                                string bindingProperty = (header.Column.DisplayMemberBinding as Binding).Path.Path;
                                ListSortDirection sortDirection = ListSortDirection.Ascending;
                                if (listView.Items.SortDescriptions.Count > 0)
                                {
                                    SortDescription sd = listView.Items.SortDescriptions[0];
                                    sortDirection = (ListSortDirection)(1 - (int)sd.Direction);
                                    listView.Items.SortDescriptions.Clear();
                                }

                                SortInfo sortInfo = listView.GetValue(SortInfoPropertyKey.DependencyProperty) as SortInfo;
                                if (sortInfo == null)
                                {
                                    sortInfo = new SortInfo() { LastSortColumn = header, CurrentAdorner = new ArrowAdorner(header, new ListSortDecorator { SortDirection = sortDirection }) };
                                    AdornerLayer.GetAdornerLayer(header).Add(sortInfo.CurrentAdorner);
                                    listView.SetValue(SortInfoPropertyKey, sortInfo);
                                }
                                else
                                {
                                    if (sortInfo.LastSortColumn != header)
                                    {
                                        AdornerLayer.GetAdornerLayer(sortInfo.LastSortColumn).Remove(sortInfo.CurrentAdorner);
                                        sortInfo.LastSortColumn = header;
                                        sortInfo.CurrentAdorner = new ArrowAdorner(header, new ListSortDecorator { SortDirection = sortDirection });
                                        AdornerLayer.GetAdornerLayer(header).Add(sortInfo.CurrentAdorner);
                                    }
                                    sortInfo.CurrentAdorner.Child.SortDirection = sortDirection;
                                }
                                try
                                {
                                    listView.Items.SortDescriptions.Add(new SortDescription(bindingProperty, sortDirection));
                                }
                                catch (InvalidOperationException)
                                {
#if DEBUG
                                    throw;
#endif
                                }
                            }
                        }));
                    }
                }));
        }
예제 #3
0
 internal static void SetSortInfo(DependencyObject obj, SortInfo value)
 {
     obj.SetValue(SortInfoPropertyKey.DependencyProperty, value);
 }
예제 #4
0
 internal static void SetSortInfo(DependencyObject obj, SortInfo value)
 {
     obj.SetValue(SortInfoPropertyKey.DependencyProperty, value);
 }