0

I have a ListView with Orientation="Horizon" which means that DataTemplate will look like "column" instead of row. Everything works fine and ListView shows a scroll bar when I have a lot of columns that cant be showed in screen. I have set MinWidth for DataTemplate. What I am trying to accomplish is that in case my source has less than 3 object to expand Datatemplate's width to fit the screen of ListView. I am aware that I can get the current width of the window using:

System.Windows.SystemParameters.PrimaryScreenWidth;
System.Windows.SystemParameters.PrimaryScreenHeight;

What I need is a callback method to be executed when the size of the window is changed in order to update the DataTemplates width. Is this functionality exists in WPF? Are there are any alternatives?

BlackM
  • 3,927
  • 8
  • 39
  • 69

1 Answers1

1

What I need is a callback method to be executed when the size of the window is changed in order to update the DataTemplates width. Is this functionality exists in WPF?

You could handle the SizeChanged event for the Window or ListView.

This event will be invoked whenever the size of the element changes. You can then change the width of your columns in the event handler, e.g.:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        SizeChanged += (s, e) => 
        {
            GridView gv = listView.View as GridView;
            gv.Columns[0].Width = ...;
        };
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88