views:

335

answers:

1

I have a simple List View:

<ListView Name="Container" >

</ListView>

1) I'd like items to be listed vertically till there's space and then to fill another column (header not needed):

147
258
369

I am adding items programmatically like this - but they display horizontally and then go onto the next row when space runs out:

foreach (Object obj in myCollection)
{
  UIElement control = CreateListViewItem(obj);
  this.Container.Items.Add(control);
}

2) I also need to implement simple sorting for the list (a toggle between/ascending descending).

I am having an hard time finding answers to this apparently simple query!

Any help appreciated.

+2  A: 

1) If you don't want headers use a ListBox instead with a WrapPanel.

2) Why are you adding UIElement to a items control? Most likely you want to use data binding. Specially if you want to sort them. How do you sort UIElements?

Below are some code that uses data binding. Hope you can use it.

XAML:

<Window x:Class="ListViewTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition />
    </Grid.RowDefinitions>

    <Button Grid.Row="0" Content="Sort" Click="OnSort" />

    <ListBox Grid.Row="1" ItemsSource="{Binding Path=MyCollectionView}"
        ScrollViewer.HorizontalScrollBarVisibility="Auto" 
        ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Red" BorderThickness="2" Margin="5">
                    <TextBlock Text="{Binding}" />
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

</Grid>

Code behind:

using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace ListViewTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            List<object> myCollection = new List<object>();
            for (int i = 100; i <= 999; i++)
            {
                myCollection.Add(i);
            }

            MyCollectionView = new ListCollectionView(myCollection);
            MyCollectionView.CustomSort = _mySort;
            DataContext = this;
        }

        public ListCollectionView MyCollectionView { get; private set; }

        private void OnSort(object sender, RoutedEventArgs e)
        {
            _mySort.Ascending = !_mySort.Ascending;
            MyCollectionView.Refresh();
        }

        private MySort _mySort = new MySort();

        private class MySort : IComparer
        {
            public bool Ascending { get; set; }
            #region IComparer Members
            public int Compare(object x, object y)
            {
                return x.ToString().CompareTo(y.ToString()) * (Ascending ? -1 : 1);
            }    
            #endregion
        }
    }
}
Wallstreet Programmer
Thanks - I am beginning with WPF so what seems obvious to most people it's not obvious yet as I don't know the layout controls very well! I'll give it a shot.
JohnIdol
Just to give a bit more of info - I am basically displaying a list of tags (they need to be ordered as I show in the question) as buttons (created in the CreateListViewItem method).
JohnIdol