开发者

WPF ListView - How to get column aligment to work properly with resizable columns?

I did some googling on the this found several references which did similar things to the example below. When I try this, the first column is aligned right initialy. Without the setter for HorizontalContentAlignment the text stays put when the column is resized instead of tracking the right edge of the column. Adding the setter has the effect of centring the TextBlock in the column as it is resized but it does not stretch to fit the column. I changed the background colour of the first column to hightlight what is happening. I am using VS 2008 with version 3.5 SP1 of the framework.

<Window.Resources>
    <XmlDataProvider x:Key="CharacterData">
        <x:XData>
            <Data xmlns="">
                <Character First="Bart" Last="Simpson" />
                <Character First="Homer" Last="Simpson" />
                <Character First="Lisa" Last="Simpson" />
                <Character First="Maggie" Last="Simpson" />
                <Character First="Marge" Last="Simpson" />
            </Data>
        </x:XData>
    </XmlDataProvider>
</Window.Resources>

<ListView ItemsSource="{Binding Source={StaticResource CharacterData},
  XPath=Data/Character}" >

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>

    <ListView.View>
        <GridView>
            <GridViewColumn Width="100" Header="First Name" >
                <GridViewColumn.CellTemplate >
                    <DataTemplate>
                        <TextBlock Width="98" Margin="-6,0" TextAlignment="Right" Text="{Binding XPath=@First}" Background="Aqua" HorizontalAlignment="Stretch" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="100" Header="Last Name">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Width="100" Margin="-6,0" TextAlignment="Left" Text="{Binding XPath=@Last}" />
                    &开发者_如何学编程lt;/DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

Update: Removing the width parameter from the TextBox allows the stretching defined in the ItemContainerStyle to work properly.

<DataTemplate>
   <TextBlock  Margin="-6,0" TextAlignment="Right" Text="{Binding XPath=@First}" Background="Aqua"  />
</DataTemplate>


You need to put HorizontalContentAlignment="Stretch" onto your listview.

Update: I meant the list view itself, not the container style:

<ListView ItemsSource="{Binding Source={StaticResource CharacterData}, XPath=Data/Character}" HorizontalContentAlignment="Stretch">

Update2: I just noticed that you have set the Width of your text blocks - that's why they are not stretching. Remove the Width and replace it with MinWidth if you want a minimum width

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜