- 将ListView包裹在一个父Grid中
- 写一个与ListView平行的Grid,设置该Grid的列数与ListView中GridView的列数相同,将该Grid各列设置列宽按比例分配
- 将ListView中GridView的各列的Width绑定上述Grid的ActualWidth
- 这样,通过设置上述Grid各列的比例,就对应设置了ListView中GridView的各列的比例
1 <Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3">
2 <Grid>
3 <Grid.ColumnDefinitions>
4 <ColumnDefinition Width="3*" />
5 <ColumnDefinition Width="2*" />
6 <ColumnDefinition Width="8*"/>
7 <ColumnDefinition Width="5*"/>
8 <ColumnDefinition Width="5*"/>
9 </Grid.ColumnDefinitions>
10 <Grid Grid.Column="0" x:Name="HumidifyEventLogCol0"/>
11 <Grid Grid.Column="1" x:Name="HumidifyEventLogCol1"/>
12 <Grid Grid.Column="2" x:Name="HumidifyEventLogCol2"/>
13 <Grid Grid.Column="3" x:Name="HumidifyEventLogCol3"/>
14 <Grid Grid.Column="3" x:Name="HumidifyEventLogCol4"/>
15 </Grid>
16
17 <ListView Name="HumidifyEventLog" Style="{StaticResource ListViewStyle}" ItemContainerStyle="{StaticResource ResourceKey=ListViewItemStyle}">
18 <ListView.View>
19 <GridView ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle}">
20 <GridViewColumn Header="设备" Width="{Binding ElementName=HumidifyEventLogCol0,Path=ActualWidth}" DisplayMemberBinding="{Binding Path=Source, Converter={StaticResource ChamberNameEnToCn}}"/>
21 <GridViewColumn Header="故障ID" Width="{Binding ElementName=HumidifyEventLogCol1,Path=ActualWidth}" DisplayMemberBinding="{Binding Path=Code}"/>
22 <GridViewColumn Header="故障描述" Width="{Binding ElementName=HumidifyEventLogCol2,Path=ActualWidth}" DisplayMemberBinding="{Binding Path=Remark}"/>
23 <GridViewColumn Header="故障开始时间" Width="{Binding ElementName=HumidifyEventLogCol3,Path=ActualWidth}" DisplayMemberBinding="{Binding Path=StartTime}"/>
24 <GridViewColumn Header="故障结束时间" Width="{Binding ElementName=HumidifyEventLogCol4,Path=ActualWidth}" DisplayMemberBinding="{Binding Path=EndTime}"/>
25 </GridView>
26 </ListView.View>
27 </ListView>
28 </Grid>
参考:WPF GridView的宽度设置为按比例分配