Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4240218
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:08:12+00:00 2026-05-21T03:08:12+00:00

I am trying to get this code (from a blog) working. The download file

  • 0

I am trying to get this code (from a blog) working.

The download file isn’t available there so I copied the pieces of the code and it seems to work except that I had to make up a binding but the items do not show up.

Any ideas on how to fix this? Sounds like a trivial thing I am missing.

<Window x:Class="WpfMultiColumnSorting.SortWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SortWindow" Height="300" Width="600">
    <ListView Name="DashListView" ItemsSource="{Binding SortedItems}">

        <ListView.View>
            <GridView>
                <GridViewColumn Width="95" DisplayMemberBinding="{Binding Name}">
                    <GridViewColumnHeader Click="SortClick" Tag="Name" Content="Name" />
                </GridViewColumn>

                <GridViewColumn Width="90">
                    <GridViewColumnHeader Click="SortClick" Tag="IsActive" Content="Activitation" />
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" ToolTip="{Binding IsActive}">
                                <TextBlock Name="ActivityStatusText" Text="{Binding IsActive}"></TextBlock>
                            </StackPanel>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsActive}" Value="true">
                                    <Setter TargetName="ActivityStatusText" Property="FontWeight" Value="Bold" />
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Width="140" DisplayMemberBinding="{Binding CreationDate}">
                    <GridViewColumnHeader Click="SortClick" Tag="CreationDate" Content="Created On" />
                </GridViewColumn>

                <GridViewColumn Width="85" DisplayMemberBinding="{Binding Value}">
                    <GridViewColumnHeader Click="SortClick" Tag="Value" Content="Value #" />
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Window>





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Globalization;
using System.Collections.ObjectModel;

namespace WpfMultiColumnSorting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class SortWindow : Window
    {
        ObservableCollection<SortedItemViewModel> sortedItems;
        public ObservableCollection<SortedItemViewModel> SortedItems
        {
            get { return this.sortedItems; }
            set
            {
                this.sortedItems = value;
                this.RaisePropertyChanged ( "SortedItems" );
            }
        }

        public SortWindow ( )
        {
            this.SortedItems = new ObservableCollection<SortedItemViewModel> ( );

            this.SortedItems = new ObservableCollection<SortedItemViewModel> ( ) {
                new SortedItemViewModel ( "A", false, DateTime.Now, "1" ),
                new SortedItemViewModel ( "B", false, DateTime.Now, "2" ),
                new SortedItemViewModel ( "C", false, DateTime.Now, "3" ),
                new SortedItemViewModel ( "D", false, DateTime.Now, "4" ),
                new SortedItemViewModel ( "E", false, DateTime.Now, "5" ),
                new SortedItemViewModel ( "F", false, DateTime.Now, "6" ),
                new SortedItemViewModel ( "G", false, DateTime.Now, "7" )
            };
        }

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;
        void RaisePropertyChanged ( string propertyName )
        {
            var handler = this.PropertyChanged;
            if ( handler != null )
                handler ( this, new PropertyChangedEventArgs ( propertyName ) );
        }

        #endregion

        private List<GridViewColumnHeader> _sortColumnStack = new List<GridViewColumnHeader> ( );

        private IDictionary<GridViewColumnHeader, SortAdorner> _adorners = new Dictionary<GridViewColumnHeader, SortAdorner> ( );


        private void SortClick ( object sender, RoutedEventArgs e )
        {
            GridViewColumnHeader column = sender as GridViewColumnHeader;
            ListSortDirection newDir = ListSortDirection.Ascending;

            // Control means clear out all the prior sort stack
            bool clear = Keyboard.Modifiers == ModifierKeys.Control;

            PerformSort ( column, newDir, clear );
        }


        /// <summary>
        /// Updates the sort stack
        /// </summary>
        /// <param name="column">The columkn on which we are sorting</param>
        /// <param name="newDir">The direction we should place the column sort into</param>
        /// <param name="clear">true to clear out all curent sorting</param>
        private void PerformSort ( GridViewColumnHeader column, ListSortDirection newDir, bool clear )
        {
            IDictionary<GridViewColumnHeader, SortAdorner> oldAdorners = new Dictionary<GridViewColumnHeader, SortAdorner> ( );

            // determine if we are clicking an already sorted column
            if ( _sortColumnStack.Contains ( column ) && _adorners.ContainsKey ( column ) )
            {
                if ( _sortColumnStack [ _sortColumnStack.Count - 1 ] != column )
                {
                    // if it is not the primary use the same direction
                    newDir = _adorners [ column ].Direction;
                }
                else
                {
                    // if it is the primary column then flip it
                    if ( newDir == _adorners [ column ].Direction )
                        newDir = ListSortDirection.Descending;
                }
            }

            // remove all the adorners so we can start a fresh
            foreach ( GridViewColumnHeader col in _adorners.Keys )
            {
                // save into our old adorners list
                oldAdorners [ col ] = _adorners [ col ];
                // then remove it
                AdornerLayer.GetAdornerLayer ( col ).Remove ( _adorners [ col ] );
            }
            _adorners.Clear ( );

            // get rid of all the sorting
            DashListView.Items.SortDescriptions.Clear ( );

            // Control means clear out all the prior sort stack
            if ( clear )
            {
                _sortColumnStack.Clear ( );
            }
            else
            {
                // remove ourselves from the stack if we are in it
                if ( _sortColumnStack.Contains ( column ) )
                {
                    _sortColumnStack.Remove ( column );
                }
            }

            // add our brand new primary one
            _sortColumnStack.Add ( column );

            // now re-create the adorners from our stack
            for ( int i = _sortColumnStack.Count - 1; i >= 0; i-- )
            {
                GridViewColumnHeader col = _sortColumnStack [ i ];
                SortAdorner adorner;

                if ( i == _sortColumnStack.Count - 1 )
                {
                    // create a new one (for the last one)
                    adorner = new SortAdorner ( col, newDir, col.Tag as string, _sortColumnStack.Count - i );
                }
                else if ( oldAdorners.ContainsKey ( col ) )
                {
                    // re-use the old adorner information
                    SortAdorner oldAdorner = oldAdorners [ col ];
                    adorner = new SortAdorner ( col, oldAdorner.Direction, col.Tag as string, _sortColumnStack.Count - i );
                }
                else
                {
                    // create a new one (for the last one)
                    adorner = new SortAdorner ( col, newDir, col.Tag as string, _sortColumnStack.Count - i );
                }

                _adorners.Add ( col, adorner );
                AdornerLayer.GetAdornerLayer ( col ).Add ( _adorners [ col ] );
                DashListView.Items.SortDescriptions.Add ( new SortDescription (
                _adorners [ col ].Field, _adorners [ col ].Direction ) );
            }
        }
    }
}





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WpfMultiColumnSorting
{
    public class SortedItemViewModel : INotifyPropertyChanged
    {
        string name;
        public string Name
        {
            get { return name; }
            set
            {
                this.name = value;
                this.RaisePropertyChanged ( "Name" );
            }
        }

        public bool IsActive { get; set; }
        public DateTime CreationDate { get; set; }
        public string Value { get; set; }

        public SortedItemViewModel ( string name, bool isActive, DateTime creationDate, string value )
        {
            this.Name = name;
            this.IsActive = isActive;
            this.CreationDate = creationDate;
            this.Value = value;
        }

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;
        void RaisePropertyChanged ( string propertyName )
        {
            var handler = this.PropertyChanged;
            if ( handler != null )
                handler ( this, new PropertyChangedEventArgs ( propertyName ) );
        }

        #endregion
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Media;
using System.ComponentModel;
using System.Windows;
using System.Globalization;

namespace WpfMultiColumnSorting
{
    public class SortAdorner : Adorner
    {
        private readonly static Geometry AscGeometry = Geometry.Parse ( "M 0,0 L 8,0 L 4,5 Z" );

        private readonly static Geometry DescGeometry = Geometry.Parse ( "M 0,5 L 8,5 L 4,0 Z" );

        public ListSortDirection Direction { get; private set; }

        public string Field { get; private set; }

        public int SortOrder { get; private set; }

        public SortAdorner ( UIElement element, ListSortDirection dir, string field, int order )
            : base ( element )
        {
            Direction = dir;
            Field = field;
            SortOrder = order;
        }

        protected override void OnRender ( DrawingContext drawingContext )
        {
            base.OnRender ( drawingContext );

            if ( AdornedElement.RenderSize.Width < 20 )
                return;

            drawingContext.PushTransform ( new TranslateTransform (
            AdornedElement.RenderSize.Width - 15, ( AdornedElement.RenderSize.Height - 5 ) / 2 ) );

            drawingContext.DrawGeometry ( SortOrder == 1 ? Brushes.Black : Brushes.DarkGray, null,
            Direction == ListSortDirection.Ascending ? AscGeometry : DescGeometry );

            // annotate each arrow adorner with a number if it is not the first one
            if ( SortOrder != 1 )
            {
                drawingContext.DrawText ( new FormattedText (
                SortOrder.ToString ( ), CultureInfo.GetCultureInfo ( "en-us" ),
                FlowDirection.LeftToRight, new Typeface ( "Verdana" ), 9,
                Brushes.Black ), new Point ( 7, 0 ) );
            }

            drawingContext.Pop ( );
        }
    }
}

No compile errors, or runtime errors and the listview is empty.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:08:13+00:00Added an answer on May 21, 2026 at 3:08 am

    It looks like you’re not setting the DataContext for the ListView. Create a new class called SortWindowViewModel and move most of your code in SortWindow.xaml.cs to this new class. Add a Loaded event handler in SortWindow.xaml.cs and add
    DataContext = new SortWindowViewModel();
    The bindings look fine, it’s just missing where those bindings are pointed to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
hI, I'm trying to get this code from Larry Nyhoff's book to compile in
I'm trying to get this piece of code working a little better. I suspect
I've been trying to get this code to work for hours! All I need
I have this piece of code I'm trying to get to display but no
I'm trying to get the exact equivalent (not functional) of this vb.net code in
Im trying to get pretty.js to prettify code in CODE tags, using this js:
I'm trying to get this simple PowerShell script working, but I think something is
I'm trying to get this sample for AJAX to WCF working, with the following
I'm trying to use the django-voting tutorial from this blog: http://new.justinlilly.com/blog/2008/nov/04/django-voting-a-brief-tutorial/ to get a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.