locked
Listview Datatemplate Horizontalalignment RRS feed

  • Question

  • I have a Listview which uses Gridview Columns. These columns use Datatemplates. I'm having some issue with getting the items to align horizontally the way I want. I thought that all I needed to do was set the HorizontalAlignment property on the control inside the Datatemplate. However, this doesn't seem to have any effect. I found a post on msdn which suggests setting the HorizontalContentAlignment property on the ListViewItem template instead. This works (it does set the alignment), but it applies the same alignment to every control in each listviewitem. I want to center some things and stretch others. For example, one column would have checkboxes which I would like to be centered, but another column would have textboxes which I would prefer stretch. That means working with the datatemplate (unless someone here knows something I don't).

    Has anyone else run into this issue? Any suggestions?
    Thursday, April 3, 2008 3:28 PM

Answers

  • Our apologies Stacy, even I was not able to figure this out (and I have been in this for some time now :)).

    Kudos to whoever wrote this blog - really cool stuff: http://www.designerwpf.com/2007/12/17/how-do-i-center-something-in-my-listview-column/

    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication101.Window1"
    x:Name="Window"
    Title="Window1"
    Width="640" Height="480">
    <Window.Resources>
    <Style TargetType="{x:Type ListViewItem}">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
    <DataTemplate x:Key="CheckBoxCellTemplate">
    <Grid Background="Green">
    <Rectangle Fill="Blue" Height="20" Width="20" HorizontalAlignment="Center"/>
    </Grid>
    </DataTemplate>
    </Window.Resources>
    <Grid x:Name="LayoutRoot">
    <ListView Margin="68,88,85,87" IsSynchronizedWithCurrentItem="True">
    <ListView.View>
    <GridView>
    <GridViewColumn Width="200" Header="CheckBox" CellTemplate="{DynamicResource CheckBoxCellTemplate}"/>
    <GridViewColumn Width="200" Header="TextBox"/>
    </GridView>
    </ListView.View>
    <ListViewItem Content="ListViewItem"/>
    </ListView>
    </Grid>
    </Window>
    Saturday, April 5, 2008 2:20 AM
    Moderator

All replies

  • Our apologies Stacy, even I was not able to figure this out (and I have been in this for some time now :)).

    Kudos to whoever wrote this blog - really cool stuff: http://www.designerwpf.com/2007/12/17/how-do-i-center-something-in-my-listview-column/

    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication101.Window1"
    x:Name="Window"
    Title="Window1"
    Width="640" Height="480">
    <Window.Resources>
    <Style TargetType="{x:Type ListViewItem}">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
    <DataTemplate x:Key="CheckBoxCellTemplate">
    <Grid Background="Green">
    <Rectangle Fill="Blue" Height="20" Width="20" HorizontalAlignment="Center"/>
    </Grid>
    </DataTemplate>
    </Window.Resources>
    <Grid x:Name="LayoutRoot">
    <ListView Margin="68,88,85,87" IsSynchronizedWithCurrentItem="True">
    <ListView.View>
    <GridView>
    <GridViewColumn Width="200" Header="CheckBox" CellTemplate="{DynamicResource CheckBoxCellTemplate}"/>
    <GridViewColumn Width="200" Header="TextBox"/>
    </GridView>
    </ListView.View>
    <ListViewItem Content="ListViewItem"/>
    </ListView>
    </Grid>
    </Window>
    Saturday, April 5, 2008 2:20 AM
    Moderator
  • Yes, that's what I figured out over the weekend. Apparently I needed to leave the HorizontalContentAlignment property of the ListViewItem set and then I could set the alignment of each datatemplate to wahtever I wanted. Thanks.
    Monday, April 7, 2008 12:14 PM