wpf ListView 列宽自动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19496893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:54:42  来源:igfitidea点击:

ListView Column Width Auto

c#wpfxamllistviewautosize

提问by user188

I wrote code in XAML - WPF Browser Application - Page , just one Listviewand one buttonto add new data to the listview(from other file), I am trying to make the first column auto size itself when the button is pushed, I am using Visual Studio c# 2010.

我在 XAML - WPF Browser Application - Page 中编写了代码,只需一个Listview又一个button地将新数据添加到listview(来自其他文件),我试图在按下按钮时使第一列自动调整大小,我正在使用 Visual Studio C# 2010。

I've used the following method in the code behind, but AutoResizeColumnswon't be recognized and gives an error.

我在后面的代码中使用了以下方法,但AutoResizeColumns不会被识别并给出错误。

Unfortunately, none of the previously suggested solutions worked with me.

不幸的是,之前建议的解决方案都没有对我有用。

The Code Behind

背后的代码

public partial class Page1 : Page, INotifyPropertyChanged
{

    public Page1()
    {

        InitializeComponent();
        this.DataContext = new Page1Model();

    }

    private void TestListe_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void Button1_Click(object sender, RoutedEventArgs e)

   {
        TestListe1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
   }

}

XAML

XAML

<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <ListView Name="TestListe1"   Margin="68,22,421,8"  FontSize="12"  >

        <ListView.View>
            <GridView>
                <GridView.Columns>
                      <GridViewColumn Width="auto">                                <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="ST1" Margin="10,0,10,1"/>
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding One}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                                        </GridView.Columns>
            </GridView>
        </ListView.View>
   </ListView>

    <Button Name="Button1" Grid.Row="1" Height="27" Width="95" Margin="262,24,444,74"  Click="Button1_Click" />

</Grid>

回答by Harrison

AutoResizeColumnsis from the namespace System.Windows.Forms. I'm not sure if that will work with WPF or not. But you can set the width of the column to NANto make it resize

AutoResizeColumns来自命名空间System.Windows.Forms。我不确定这是否适用于 WPF。但是您可以将列的宽度设置为NAN调整大小

In your XAMLif you name your GridView as follows:

在你的XAML如果你命名你的 GridView 如下:

<GridView x:Name="dataGridView">

Then you could resize all columns with this

然后你可以用这个调整所有列的大小

 foreach (GridViewColumn c in dataGridView.Columns)
 {
     c.Width = 0; //set it to no width
     c.Width = double.NaN; //resize it automatically
 }