页面
<ListView SizeChanged="ListView_SizeChanged" Loaded="ListView_Loaded" >
<ListView.View>
<GridView>
<GridViewColumn Header="col1" Width="100" />
<GridViewColumn Header="col1" Width="Auto" />
<GridViewColumn Header="col1" />
</GridView>
</ListView.View>
</ListView>
cs文件
private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateColumnsWidth(sender as ListView);
}
private void ListView_Loaded(object sender, RoutedEventArgs e)
{
UpdateColumnsWidth(sender as ListView);
}
private void UpdateColumnsWidth(ListView listView)
{
int autoFillColumnIndex = (listView.View as GridView).Columns.Count - 1;
if (listView.ActualWidth == Double.NaN)
listView.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
double remainingSpace = listView.ActualWidth;
for (int i = 0; i < (listView.View as GridView).Columns.Count; i++)
if (i != autoFillColumnIndex)
remainingSpace -= (listView.View as GridView).Columns[i].ActualWidth;
(listView.View as GridView).Columns[autoFillColumnIndex].Width = remainingSpace >= 0 ? remainingSpace : 0;
}
参考
标签:ListView,sender,GridView,去除,wpf,listView,Columns,View From: https://blog.51cto.com/ives/5949871