I am using this method to resize a ListView adjusting its columns to the space each one needed and adding the remaining space to the second column, so that no horizontal scroll bar appears.
private void SetColumnWidths(object sender)
{
ListView listView = (sender as ListView);
if (listView != null)
if (listView.IsLoaded)
{
listView.UpdateLayout();
GridView gridView = listView.View as GridView;
if (gridView != null)
{
UpdateColumnWidths(gridView);
listView.UpdateLayout();
Decorator border = VisualTreeHelper.GetChild(listView, 0) as Decorator;
if (border != null)
{
ScrollViewer scroller = border.Child as ScrollViewer;
if (scroller != null)
{
ItemsPresenter presenter = scroller.Content as ItemsPresenter;
if (presenter != null)
{
double columnWidth = presenter.ActualWidth - 2;
for (int i = 0; i < gridView.Columns.Count; i++)
{
if (i != 1)
columnWidth -= gridView.Columns[i].ActualWidth;
}
if 开发者_Python百科(columnWidth < 0)
columnWidth = 0;
gridView.Columns[1].Width = columnWidth;
}
}
}
}
}
}
private void UpdateColumnWidths(GridView gridView)
{
foreach (var column in gridView.Columns)
{
if (double.IsNaN(column.Width))
{
column.Width = 0;
column.Width = double.NaN;
}
}
}
Everything is working just fine... until BorderThickness
comes to town, either for ListView or even worst for the template of ListViewItem. What I want you to help me is on how I can retrieve the BorderThickness
for the template of ListViewItem so that I start my resize process without including it.
Or even better do you have any other idea how I can figure out the real usable area of the ListView, without borders, scrollers, mice, cats, dogs, etc?
Thank you in advance!
Why don't you set your 2nd Column Width = *
and all other Column Width's = Auto
? This will make all columns take up however much space they need, and the 2nd column will take up all remaining space.
I didn't find any other solution instead of setting my border's thickness inside my ListViewItem
template to 0 for left and right sides. With that my column size adjustments were ok. Not the best solution, but ok...
精彩评论