WPF ListView的基本布局问题
我有一个简单的列表视图:WPF ListView的基本布局问题,wpf,sorting,listview,layout,Wpf,Sorting,Listview,Layout,我有一个简单的列表视图: <ListView Name="Container" > </ListView> 我以如下方式以编程方式添加项目-但它们水平显示,然后在空间用完时转到下一行: foreach (Object obj in myCollection) { UIElement control = CreateListViewItem(obj); this.Container.Items.Add(control); } 2)我还需要实现列表的简单排序(升
<ListView Name="Container" >
</ListView>
我以如下方式以编程方式添加项目-但它们水平显示,然后在空间用完时转到下一行:
foreach (Object obj in myCollection)
{
UIElement control = CreateListViewItem(obj);
this.Container.Items.Add(control);
}
2)我还需要实现列表的简单排序(升序/降序之间的切换)
我很难找到这个看似简单的问题的答案
感谢您的帮助。1)如果您不想要标题,请使用带有包装的列表框
2) 为什么要将UIElement添加到items控件?您很可能希望使用数据绑定。特别是如果你想把它们分类的话。如何对UIElements进行排序
下面是一些使用数据绑定的代码。希望你能使用它
XAML:
代码隐藏:
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
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
名称空间ListViewTest
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
List myCollection=新列表();
对于(int i=100;我需要看更多的代码。从您的代码片段来看,似乎您正在将UIElements添加到另一个容器-而不是您的ListView.corrected type-我的ListView名称是container。这是我所有代码的90%。除了在我的页面的构造函数中,我没有做太多其他事情。我的意思是键入错误,而不是在命令中键入nt以上感谢-我从WPF开始,所以对大多数人来说很明显的东西还不明显,因为我不太了解布局控件!我来试一试。只是为了提供更多的信息-我基本上是以按钮(在CreateListViewItem方法中创建)的形式显示标签列表(它们需要按照我在问题中显示的顺序排列)。
<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
}
}
}