列变小时调整ListView的大小

时间:2012-09-10 07:18:37

标签: c# wpf xaml

我有一个listview,当我手动调整列宽时,整个listview变得更大,到目前为止一直很好。但是当我缩小列时,listview stil保持宽度为。 listview是否也可以缩小列宽?

以下是一个简单的例子:

<Grid>
        <Grid.Resources>
            <XmlDataProvider x:Key="Data">
                <x:XData>
                    <Data xmlns="">
                        <Item ID="1"/>
                        <Item ID="2"/>
                        <Item ID="3"/>
                    </Data>
                </x:XData>
            </XmlDataProvider>
        </Grid.Resources>

        <ScrollViewer>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <ListView DataContext="{Binding Source={StaticResource Data}, XPath=/Data}"
                      ItemsSource="{Binding XPath=Item}" SelectionMode="Single">                  
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="ID" DisplayMemberBinding="{Binding XPath=@ID}"/>
                    </GridView>
                </ListView.View>
            </ListView>

            <TextBlock Text="Test" Grid.Column="1"/>
            </Grid>
        </ScrollViewer>
    </Grid>

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,但它并不是最好的解决方案,但它对我有用。我在列之间添加了一个gridsplitter,并将列宽设置为*。

<Grid>
        <Grid.Resources>
            <XmlDataProvider x:Key="Data">
                <x:XData>
                    <Data xmlns="">
                        <Item ID="1"/>
                        <Item ID="2"/>
                        <Item ID="3"/>
                    </Data>
                </x:XData>
            </XmlDataProvider>
        </Grid.Resources>

        <ScrollViewer>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="3"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <ListView DataContext="{Binding Source={StaticResource Data}, XPath=/Data}"
                      ItemsSource="{Binding XPath=Item}" SelectionMode="Single">                  
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="ID" DisplayMemberBinding="{Binding XPath=@ID}"/>
                    </GridView>
                </ListView.View>
            </ListView>

            <GridSplitter Grid.Column="1" Width="3" VerticalAlignment="Stretch" HorizontalAlignment="Center" Background="Transparent" ResizeBehavior="PreviousAndNext"/>


            <TextBlock Text="Test" Grid.Column="2"/>
        </Grid>
        </ScrollViewer>
    </Grid>