Multiselect on GridView in WPF ListView loses selection on mouse click

We overrode the ListViews View using a GridView so that we could display the data in the format our users required. The issue that we encountered was that after we introduced MultiSelect, our drag and drop didn’t work as expected.

You could select multiple rows, but when you then clicked on the selection area to “drag” them, then it would unselect everything but the one you specifically clicked on.

Not the greatest of behaviors.

So the trick is rather simple, yet strange. Override the ListViewItem class, and use it to override the generated items.

Your new ListView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Controls.Primitives; 

namespace Seekford{
    public class MyListView: ListView    {
        protected override DependencyObject GetContainerForItemOverride()
        {            return new MyListItem();
     protected override bool IsItemItsOwnContainerOverride(object item)
           return item is MyListItem;


Your new ListViewItem

using System;
using System.Windows.Controls;
using System.Windows.Input;
using System.ComponentModel;

namespace Seekford{
      public class MyListItem : ListViewItem	{

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            if (IsSelected)

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)

Nice and easy! Kludgy, but seems to work.

Happy Coding!

How to put bound data (Binding) into a Grid View Header or List View Header.

This is another seemingly simple task. It actually is, but you may go down the route of setting the GridViewHeader.Template and wondering why it doesn’t work.

Well, it’s as simple as putting a GridViewHeader into the GridViewColumn definition and setting content to it.

<GridViewColumn  Width="Auto">
        <Grid HorizontalAlignment="Right" >
                <TextBlock Text="{Binding Title, Mode=OneWay}" Grid.Row="0" HorizontalAlignment="Right" />

See. Nice and easy right? Just fill in the different UI controls you want to make it fit your needs and away you go.

Happy Coding!

How to left align or right align the header in a ListView or GridView in WPF

So you have this great ListView or GridView in your fancy WPF application, but everything is centered. Maybe you like that, and all is good. Or, like most of us, you want the data aligned to the left or right. I like the left for most data, makes more sense. You looked for the property, and hmm… not to be found.

It turns out this seemingly simple task is, well, actually quite simple. It is really easy to spend a number of hours looking for the “magic” tag, property, etc. to do this. You will find out though, it’s all about having style.

You simply need to set the HorizontalContentAlignment on the style for the header to align everything however you want to.

Take a look at the following code:

<GridViewColumn  Width="Auto"  >
    <Style  TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Right" />

What we are doing is setting the HorizontalContentAlignment to right aligned. This will cause the contents of the header to all shift to the right. If you are using your own template, make sure to right align the textblocks and other controls as well. Otherwise, it probably won’t look right.

Nice and easy, right?

Happy Coding!

Thanks JambiHacker

I guess I was somehow important enough to be hacked by the Jambi Hacker Link. Some yahoo’s out of Indonesia.  WordPress security must be a piece of crap.

Time for server lock down. Good times.