GridviewColumn width adjusts for first entry only; when set to auto - WPF

1.6k views Asked by At

I am trying to achieve following things
1. Set Minimum width for gridview column to some value.
2. When entries are added to this column, column width should adjust accordingly.
I have written following code for it

<Grid Background="white" Height="140" Width="auto">
            <ListView Style="{StaticResource stl_lvi_TEXT}" Grid.Row="0" Grid.Column='0' Name="lv_includedInstruments" SelectionMode="Single" Background="White" Height="140" Width="auto" HorizontalAlignment ="Stretch"  VerticalAlignment="Stretch" ItemsSource="{Binding Coll_History}" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="lv_includedInstruments_SelectionChanged" TabIndex="18" GotFocus="lv_includedInstruments_GotFocus">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="212"  >
                            <GridViewColumn.Header>
                                <GridViewColumnHeader Style="{StaticResource stl_ColumnHeaderStyle}" Content="Groups" HorizontalContentAlignment="Left" Background="white" FontSize="12" BorderBrush="#FF97BADA" BorderThickness="1" Tag="ProtocolVisitTypeForm.ProtocolName"/>
                            </GridViewColumn.Header>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Width="auto" Tag="{Binding}" Text="{Binding Path=ProtocolVisitTypeForm.ProtocolName}" >
                                    </TextBlock>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>  

stl_ColumnHeaderStyle is as follows

<Style x:Key="stl_ColumnHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock MinWidth="200" Width="auto" TextWrapping="Wrap" Text="{Binding}"></TextBlock>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

this works fine when i add first entry of string of lenght x. But if subsequent entries are of greater than length x, then the column width does not expand.
Thank You.

2

There are 2 answers

0
Erti-Chris Eelmaa On

You are already setting gridVIewColumn width explictly. It will never grow more than 212. Bind GridViewColumn.Width against GridViewColumn.Header.ActualWidth. It should be dynamic then.

0
Sisyphe On

EDIT: If your Coll_History is observable (and I guess it is since you are binding on it) you can do something like this :

  • Set the minimum Width you want as GridViewColumn Width (212 in your case)
  • Remove the minWidth from the header data template if you don't need it

In your xaml : add a handler to the Loaded event of the ListView control :

<ListView Loaded="lv_includedInstruments_Loaded" ... > ... </ListView>

In the code behind :

private void lv_includedInstruments_Loaded(object sender, RoutedEventArgs e)
{
    ((sender as ListView).Items as INotifyCollectionChanged).CollectionChanged += ListViewItemsCollectionChanged;
}

void ListViewItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    var oldItems = e.OldItems;
    var newItems = e.NewItems;
    if ((oldItems == null || oldItems.Count == 0) && (newItems != null && newItems.Count > 0))
        gridViewColumn.Width = double.NaN;
}