简体   繁体   English

WPF - Listview数据绑定列宽问题

[英]WPF - Listview Databound Column Width Problem

Given below xaml using .Net3.5 - 给出下面的xaml使用.Net3.5 -

<ListView Name="RawData" AlternationCount="2" ItemsSource="{Binding}"
          ScrollViewer.VerticalScrollBarVisibility="Visible" Grid.Row="1"
          Grid.ColumnSpan="4"
          IsSynchronizedWithCurrentItem="True"
          sorter:GridViewSort.AutoSort="True"
          sorter:GridViewSort.ShowSortGlyph="False" >
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="LightGray"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=GetDate}"
                            Width="Auto" sorter:GridViewSort.PropertyName="GetDateTime"/>
            <GridViewColumn Header="Time" DisplayMemberBinding="{Binding Path=GetTime}"
                            Width="Auto" sorter:GridViewSort.PropertyName="GetDateTime"/>
            <GridViewColumn Header="Scan Time" DisplayMemberBinding="{Binding Path=ScanTimeSec}"
                            sorter:GridViewSort.PropertyName="ScanTimeSecond"  Width="Auto"/>
            <GridViewColumn Header="Ping Time" DisplayMemberBinding="{Binding Path=PingTimeSec}"
                            sorter:GridViewSort.PropertyName="PingTimeSec"  Width="Auto" />
            <GridViewColumn Header="Fault Messages"
                            DisplayMemberBinding="{Binding Path=ErrorMessages}"
                            sorter:GridViewSort.PropertyName="ErrorMessages" Width="Auto"/>
        </GridView>
    </ListView.View>
</ListView>

My problem is with the first and last columns. 我的问题是第一列和最后一列。 They are not automatically resizing to fit the contents of the data. 它们不会自动调整大小以适应数据的内容。 I could use a fixed width, but that will cause problems later in the project when I let users adjust font size. 我可以使用固定的宽度,但是当我让用户调整字体大小时,这会在项目后期引起问题。

Every solution I've found seems to be to set the width="auto" and WPF will do this for you. 我发现的每个解决方案似乎都是设置width =“auto”,WPF会为你做这个。 However, it doesn't do it. 但是,它没有这样做。 So as a bit of a newbie at WPF, I'm not sure what to do. 所以作为WPF的新手,我不知道该怎么办。

please remove the auto property for Width.You can manually set the width.Like Width="100". 请删除Width的自动属性。您可以手动设置width.Like Width =“100”。 I fetched just like this problem and solve by manually enter the width. 我就像这个问题一样取出并通过手动输入宽度来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM