在StackPanel中WPF的元素排序 [英] sorting elements in a stackpanel WPF

查看:452
本文介绍了在StackPanel中WPF的元素排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用户控件,可在运行过程中添加或删除一个StackPanel中。
这些元素都有,我为他们分配的指标,当我新的他们,我需要保持这些元素通过索引排序的,所以我沃特,基于该指数
,但上线排序他们快速排序功能,做了交换。

I have a stackpanel with some usercontrols that are added or removed during runtime. These elements have an index that i assign to them when i new them, I need to keep these elements sorted by that index so i wote a quicksort function that sorts them based on the index but on the line that does the swapping

          y = items[i]; //y is a temp variable
          items[i] = items[j];



我得到

I get

指定的索引已经在使用。断开Visual子级指定索引处第一

"Specified index is already in use. Disconnect the Visual child at the specified index first"

我试着将它们复制到一个临时变量,从集合中删除它们,然后将它们分配给他们的权利指数随UIElementCollection插入函数,但我得到

I tried copying them to a temp variable, delete them from the collection and then assign them to their right index with the Insert function in the UIElementCollection, but then I Get

规定的目视已经是另一个Visual的孩子或CompositionTarget的根

"Specified Visual is already a child of another Visual or the root of a CompositionTarget"

有没有办法,我需要什么我思念的地方克隆元素?

Is there a clone element that i need or something im missing somewhere?

推荐答案

为什么不要使用一个ListBox / ItemsSontrol并使用它CollectionViewSorce / SortDescription得到这个工作完成。添加元素的布局面板一样的StackPanel不是在WPF应用去的有效方法。 StackPanel中垂直方向是列表框/ ItemsControl的默认值,但是如果你想要一些不同的布局你总是可以覆盖ListBox.ItemsPanel模板

Why dont you use a ListBox/ItemsSontrol and use CollectionViewSorce/SortDescription on it to get this work done. Adding an elements to the LayOut Panels like stackpanel is not an efficient way to go in WPF apps. StackPanel with vertical orientation is the default for ListBox/ItemsControl,but if you want some different layout you can always override ListBox.ItemsPanel Template

如果你可以按照 < A HREF =htt​​p://msdn.microsoft.com/en-us/magazine/dd419663.aspx> MVVM apporach那么它指定一个属性(你的情况的问题这将非常具有的首页的)在你的ViewModel类,并设置SortDescription在列表框水平会自动给你该功能。相反,添加和删除实际UI元素的,你只需要添加/删除到的ObservableCollection绑定到ListBox.ItemsSource。 。并指定适当的DataTemplate

If you can follow MVVM apporach then it is a matter of specifying a property(in your case it willbe Index) in your ViewModel class and set SortDescription at the listBox level will automatically give you this feature. Instead of adding and removing actual UIElements, you need just to add/remove to the ObservableCollection bind to ListBox.ItemsSource. And specify proper DataTemplate.

检查这个,如果你的arent熟悉CollectionViewSource - 的 http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.sortdescriptions.aspx

Check this if you arent familiar with CollectionViewSource - http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.sortdescriptions.aspx

和代码会更喜欢下面..这是非常简单的。

And the code will be more like below.. it is pretty simple.

   <UserControl.Resources>
    <CollectionViewSource x:Key="sourceCollection" Source="{Binding YourObservableCollectionProperty}">
       <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="YourProperty-Index"/>
       </CollectionViewSource.SortDescriptions>
     </CollectionViewSource>
   </UserControl.Resources>       

  <ItemsControl ItemsSource="{Binding Source={StaticResource sourceCollection}}"/>



注意:< - 的xmlns:SCM =CLR的命名空间:System.ComponentModel程序;装配= WindowsBase - >

note:<--xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"-->

这篇关于在StackPanel中WPF的元素排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆