GridView dynamic column width in WPF

0 votes
asked Sep 30, 2009 by thenameisguy
<Window x:Class="GridViewSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" >
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <ListView Name="CheckoutList" Margin="0,0,8,0">
        <ListView.View>
            <GridView x:Name="CheckOutGridView">
                <GridView.ColumnHeaderContainerStyle>
                    <Style TargetType="{x:Type GridViewColumnHeader}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </Style>
                </GridView.ColumnHeaderContainerStyle>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock> 
                                <Hyperlink >          
                                    <TextBlock Text="{Binding Path=Text}"/> 
                                </Hyperlink> 
                            </TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock> 
                                <Hyperlink>          
                                    <TextBlock Text="Remove"/> 
                                </Hyperlink> 
                            </TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

    <StackPanel Grid.Row="1">
        <Button Width="100" Height="25" Click="Button_Click" >name</Button>
        <Button Width="100" Height="25" Click="Button_Click1" >long name</Button>
    </StackPanel>
</Grid>

public partial class Window1 : Window
{
    class TextObject
    {
        private string _text;

        public TextObject(string Text)
        {
            _text = Text;
        }

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
    }


    public Window1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        CheckoutList.Items.Add(new TextObject("name"));
    }

    private void Button_Click1(object sender, RoutedEventArgs e)
    {
        CheckoutList.Items.Add(new TextObject("long name"));
    }

}

Given the above sample press name then long name. How come the column doesn't resize to fit its contents?

1 Answer

0 votes
answered Sep 14, 2009 by tri-q-tran

Try

<GridViewColumn Width="{x:Static System:Double.NaN}">

where

xmlns:System="clr-namespace:System;assembly=mscorlib"

and this should help you resize the column to fit the header content.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...