2009-08-05 49 views
0

我有一个简单的列表查看:)我想项目基本布局问题

<ListView Name="Container" > 

</ListView> 

被垂直列出,直到有空间,然后以填补另一列(标题不需要) :

147 
258 
369 

我加入项目编程这样的 - 但他们水平显示,然后去到下一行,当空间用完:

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

2)我还需要实现列表的简单排序(在/升序降序之间切换)。

我很难找到这个看起来很简单的查询的答案!

任何帮助表示赞赏。

+0

需要看到一点更多的代码。从你的代码片段看来,你将UIElements添加到不同的容器 - 而不是你的ListView。 – Charlie 2009-08-05 16:49:59

+0

已更正的类型 - 我的ListView名称是Container。这是给出或占用我有的代码的90%。除了在我的页面的构造函数中,我没有做其他事情。 – JohnIdol 2009-08-05 17:30:55

+0

我的意思是* typO *不* typE *在上面的评论 – JohnIdol 2009-08-05 18:55:33

回答

2

1)如果你不想要标题使用ListBox而不是WrapPanel。

2)为什么要将UIElement添加到项目控件?很可能你想使用数据绑定。特别是如果你想对它们进行排序。你如何分类UIElements?

下面是一些使用数据绑定的代码。希望你能使用它。

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> 

后面的代码:

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 
     } 
    } 
} 
+0

谢谢 - 我开始与WPF所以对大多数人来说显然是显而易见的,因为我不知道布局控制得很好!我会给它一个镜头。 – JohnIdol 2009-08-05 21:05:59

+0

只是给了更多的信息 - 我基本上显示的标签列表(他们需要按照我在问题中显示的顺序)作为按钮(在CreateListViewItem方法中创建)。 – JohnIdol 2009-08-05 21:08:28