Posts Tagged ListView

How to set the BackgroundColor on a GridViewColumn in WPF!

So, you figured you would set the Grid or Border background color and it would work out? Then you ran your program and only the text was being filled in with your color right?

Well, the issue is two fold. First, you need to set the cell contents to stretch. Then you need to get past the fact the column pads the cell by 6 pixels left and right.

Handling the padding is here:


 <Style TargetType="Border" x:Key="columnBorder">                                <Setter Property="Margin" Value="-6,-2,-6,-6"/>                                <Setter Property="Padding" Value="6,2,6,6"/>

</Style>

 

 

Set the HorizontalContentAlignment on the ListView or GridView.

Hope this helps.

I googled around for other entries and found this one with more detail than I wanted to write.

http://www.interact-sw.co.uk/iangblog/2007/05/30/wpf-listview-column-margins

Happy coding!

, , , ,

No Comments

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)
                return;
            base.OnMouseLeftButtonDown(e);
       }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            if(IsSelected)
                base.OnMouseLeftButtonDown(e);
        }
 }
}

Nice and easy! Kludgy, but seems to work.

Happy Coding!

, , , , ,

5 Comments

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">
    <GridViewColumnHeader>
        <Grid HorizontalAlignment="Right" >
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Title, Mode=OneWay}" Grid.Row="0" HorizontalAlignment="Right" />
             </Grid>
    </GridViewColumnHeader>
</GridViewColumn>

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!

, , ,

No Comments

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"  >
 <GridViewColumn.HeaderContainerStyle>
    <Style  TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Right" />
    </Style>    
 </GridViewColumn.HeaderContainerStyle>
</GridViewColumn>

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!

, , ,

No Comments