public ListViewColumnHeaderCommandBehavior()
        {
            onColumnHeaderClick = (s, e) =>
            {
                var clickedHeader = e.OriginalSource as GridViewColumnHeader;
                if (clickedHeader != null && clickedHeader.Role != GridViewColumnHeaderRole.Padding)
                {
                    var    column       = clickedHeader.Column;
                    String commandParam = null;

                    if (column.DisplayMemberBinding is Binding)
                    {
                        commandParam = (( Binding )column.DisplayMemberBinding).Path.Path;
                    }
                    else
                    {
                        commandParam = GridViewColumnManager.GetSortProperty(column);
                    }

                    if (!String.IsNullOrEmpty(commandParam) && this.Command != null && this.Command.CanExecute(commandParam))
                    {
                        this.Command.Execute(commandParam);
                    }
                }
            };

            onLoaded = (s, e) =>
            {
                this.AssociatedObject.AddHandler(
                    GridViewColumnHeader.ClickEvent,
                    onColumnHeaderClick);
            };
        }
Exemplo n.º 2
0
        static void AdjustColumns(ListView listView)
        {
            if (ListViewManager.GetAutoSizeColumns(listView))
            {
                var gv = Ensure.That(listView.View as GridView)
                         .WithMessage("This behavior can be attached only to ListView(s) whose view is a GridView.")
                         .IsNotNull()
                         .GetValue();

                var columnsToStretch = gv.Columns.Where(c => GridViewColumnManager.GetFill(c));
                if (columnsToStretch.Any())
                {
                    var subtract = .0d;
                    var sv       = VisualTreeCrawler.FindChild <ScrollViewer>(listView);
                    if (sv != null && sv.ComputedVerticalScrollBarVisibility == Visibility.Visible)
                    {
                        var sb = VisualTreeCrawler.FindChild <ScrollBar>(sv, child => child.Orientation == Orientation.Vertical);
                        if (sb != null)
                        {
                            subtract = sb.Width;
                        }
                    }

                    var nonStretchedColumnsActualWidth = gv.Columns
                                                         .Where(c => !GridViewColumnManager.GetFill(c))
                                                         .Aggregate(0d, (w, c) =>
                    {
                        w += c.ActualWidth;
                        return(w);
                    });

                    var availableWidth = listView.ActualWidth - subtract - nonStretchedColumnsActualWidth;
                    if (availableWidth <= 0)
                    {
                        logger.Warning
                        (
                            "Columns cannot be auto sized, available size is invalid: {0}",
                            availableWidth
                        );
                    }

                    if (availableWidth > 0)
                    {
                        var columnsAvarageWidth = availableWidth / columnsToStretch.Count();
                        foreach (var column in columnsToStretch)
                        {
                            column.Width = columnsAvarageWidth;
                        }
                    }
                }
            }
        }