2009-08-05 53 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