Welcome to Manicprogrammer Sign in | Join | Help

Rediscovering the Obvious

An occasional journey through one man's perspectives as he fumbles along in the footsteps of many great men.
ListView + GridView - Selection == ItemsControl + GridViewHeaderRowPresenter + GridViewRowPresenter

This is neat. I've been annoyed for quite a while that I haven't figured out how to "enroll" things in a StackPanel into columns for alignment and consistency... I've wanted basic control layouts (no sorting, selection, etc) to have a roughly "Gridbased" alignment, but haven't been able to figure out how to do so without going up to a ListView configured with a GridView, which is too heavy and introduces issues with turning OFF the selection behavior (which, oddly, doesn't have any properties to disable it that I can find)

Today, trying to figure out how to style the GridView the way I want it, I finally stumbled on to the GridViewHeaderRowPresenter and GridViewRowPresenter. YAY! These two classes allow multiple entities to share a GridViewColumnCollection (as a resource) and (somehow... insert black WPF voodoo magic here) share column and width information. So, my code for a nicely aligned read only grid of controls (the grid has buttons, links, etc on each item) is simple, as follows... after setting it up, all of my data bindings in the CellTemplate resources work just perfectly.

<!-- In the Resources -->
     <!-- Example cell data template -->
       <DataTemplate x:Key="lineItemDescriptionTemplate">
            <TextBlock
                Style="{StaticResource pageContentTextBlock}"
                VerticalAlignment="Center"
                Text="{Binding Path=Description}"
                MinWidth="150" />
        </DataTemplate>

        <GridViewColumnCollection x:Key="gridColumns">
            <!-- Details -->
            <GridViewColumn CellTemplate="{StaticResource lineItemDetailLinkTemplate}"/>
            <!-- Remove -->
            <GridViewColumn CellTemplate="{StaticResource lineItemRemoveLinkTemplate}"/>
            <!-- Image -->
            <GridViewColumn CellTemplate="{StaticResource lineItemLookupLinkTemplate}"/>
            <!-- ... -->
        </GridViewColumnCollection>
         <DataTemplate x:Key="gridRowItemTemplate">
            <GridViewRowPresenter Columns="{StaticResource saleGridColumns}" ></GridViewRowPresenter>
        </DataTemplate>


<!-- Main page body -->
   <StackPanel DockPanel.Dock="Top">
            <GridViewHeaderRowPresenter Columns="{StaticResource gridColumns}"
                                        ColumnHeaderContainerStyle="{StaticResource columnHeaderContainerStyle}"
                                        ColumnHeaderTemplate="{StaticResource columnHeaderTextBlock}"
                                        />
            <ItemsControl x:Name="_lines2"
                          ItemTemplate="{StaticResource gridRowItemTemplate}" />
   </StackPanel>

 

 

Published Wednesday, August 08, 2007 9:47 AM by willeke

Filed under: ,

Comments

No Comments

Anonymous comments are disabled