Reputation: 89
I'm trying to make a WPF program that involves the usage of listviews that contain gridviews. I wrote some code to scale all the elements in the UI if the window size is changed so that my software is compatible with all screen sizes.
The issue that i'm having is that when i scale the window, all the items scale nicely but the columns of the listview do not scale at all. So i tried changing the width of the columns by the same factor as the one that i use to scale the rest. However i have no clue how to access this variable of the gridview.
Does anyone know how i can change the width of the columns ? This code is what scales everything in the window:
// size mainmenu elements correctly
private void MainWindow1_SizeChanged(object sender, SizeChangedEventArgs e)
{
foreach (UIElement element in MainGrid.Children)
{
if (element.GetType() == typeof(Canvas))
{
Canvas canvas = (Canvas)element;
if(canvas.Name != "canvasCategories")
{
canvas.Width = e.NewSize.Width;
canvas.Height = e.NewSize.Height;
}
double xChange = 1, yChange = 1;
if (e.PreviousSize.Width != 0)
xChange = (e.NewSize.Width / e.PreviousSize.Width);
if (e.PreviousSize.Height != 0)
yChange = (e.NewSize.Height / e.PreviousSize.Height);
foreach (FrameworkElement fe in canvas.Children)
{
if (fe is Grid == false)
{
fe.Height = fe.ActualHeight * yChange;
fe.Width = fe.ActualWidth * xChange;
Canvas.SetTop(fe, Canvas.GetTop(fe) * yChange);
Canvas.SetLeft(fe, Canvas.GetLeft(fe) * xChange);
}
}
foreach (UIElement elem in canvas.Children)
{
// TODO Scale list colums correctly
if (elem.GetType() == typeof(ListView))
{
ListView list = (ListView)elem;
foreach (ListViewItem lvitem in list.Items)
{
}
}
}
}
}
}
This part of the previous code is what should scale the columns:
foreach (UIElement elem in canvas.Children)
{
// TODO Scale list colums correctly
if (elem.GetType() == typeof(ListView))
{
ListView list = (ListView)elem;
foreach (ListViewItem lvitem in list.Items)
{
}
}
}
As you can see i have the foreach loop that gets all the listviews in the canvas but then i don't know how to continue..
Thanks in advance!
Upvotes: 1
Views: 273
Reputation: 169360
You could get a reference to the GridView
by casting the View
property of the ListView
:
ListView list = (ListView)elem;
GridView gridView = list.View as GridView;
if (gridView != null)
{
foreach (GridViewColumn column in gridView.Columns)
{
//...
}
}
Upvotes: 2