0

I want to change the number of columns in a ListView to the screen metrics, i'm using the following code for this.

    private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        this.SizeChanged += OnWindowSizeChanged;
    }


    protected void OnWindowSizeChanged(object sender, SizeChangedEventArgs e)
    {
        double newWindowHeight = e.NewSize.Height;
        double newWindowWidth = e.NewSize.Width;
        double prevWindowHeight = e.PreviousSize.Height;
        double prevWindowWidth = e.PreviousSize.Width;

        LabelXY.Content = newWindowHeight.ToString() + newWindowWidth.ToString();
    }

And set column with xaml;

    <UniformGrid Columns="5"/>

enter image description here

Everything's fine, there are 5 columns.

enter image description here

But if the window width is less than 1200 px, I want 4 columns.

Came to my mind first to make it binding, like this then i failed.

     <UniformGrid Columns="{Binding Path=ColumnNum}"/>

So if it is not binding, possible to programmatically change the number of columns with C#? Thanks...

EgoistDeveloper
  • 775
  • 2
  • 13
  • 37
  • Yes, i was tried but too sensitive. So 5 columns with a width of 1200 px - 1300 px, but when it is 1250 px, it goes directly to 4 columns and there is a lot of space around. I want to window width is less than 1200 px, I want 4 columns. – EgoistDeveloper Dec 12 '16 at 10:58
  • Not sure but have a look at [Liz answer](http://stackoverflow.com/a/5573916/5588347). – StackUseR Dec 12 '16 at 11:24
  • Now, correct me if I'm wrong, but when you tried using a wrappanel and there was space left in your design at certain resolution, that means that the elements inside don't resize / stretch to fill a panel. Analogically, this will happen with every panel/layout you use. And when it won't stretch / resize - at 1200 px you will have an element that is only partly visible, since it won't fit the screen. – Kamil Solecki Dec 12 '16 at 22:07
  • @kamil-solecki right. – EgoistDeveloper Dec 12 '16 at 22:33
  • Is that the intended behaviour? Not saying it's bad, I just thought It could be a good point. Since if it's not, then you could fix it to stretch and easily use a wrappanel (and make the objects stretch till a certain point). – Kamil Solecki Dec 12 '16 at 22:45
  • You're right. The pictures are normally very large, but I'm displaying a certain amount with the grid. Is it possible to re-dimension as% when using a wrap panel? – EgoistDeveloper Dec 12 '16 at 22:51

1 Answers1

1

Try to put

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

And in Listview ScrollViewer.HorizontalScrollBarVisibility="Disabled"

<ListView  ItemsSource="{Binding Customers}"  ScrollViewer.HorizontalScrollBarVisibility="Disabled">
wscourge
  • 10,657
  • 14
  • 59
  • 80
Abd Co
  • 11
  • 1