GridviewColumn宽度仅适用于第一个条目;当设置为auto - WPF时

时间:2012-10-11 12:44:11

标签: wpf listview width gridviewcolumn

我正在努力实现以下目标 1.将gridview列的最小宽度设置为某个值 2.当条目添加到此列时,列宽应相应调整 我已经为它编写了以下代码

<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如下

<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>

当我添加长度为x的字符串的第一个条目时,这工作正常。但如果后续条目的长度大于x,则列宽不会扩大 谢谢。

2 个答案:

答案 0 :(得分:0)

编辑: 如果你的Coll_History是可观察的(我想这是因为你绑定它)你可以这样做:

  • 将所需的最小宽度设置为GridViewColumn Width(在您的情况下为212)
  • 如果您不需要,请从标题数据模板中删除minWidth

在你的xaml中:为ListView控件的Loaded事件添加一个处理程序:

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

在背后的代码中

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;
}

答案 1 :(得分:0)

您已经明确地设置了gridVIewColumn宽度。它永远不会超过212.将GridViewColumn.Width绑定到GridViewColumn.Header.ActualWidth。它应该是动态的。