简体   繁体   English

WPF ListBox项目的对齐方式和顺序?

[英]WPF ListBox items alignment and in order?

Is it possible to fill a listBox with items which have predefined index? 是否可以使用具有预定义索引的项目填充listBox? Such as if I have collection of items with index values: 例如,如果我有带有索引值的项目集合:

  • item1, index 1 项目1,索引1
  • item2, index 2 item2,索引2
  • item3, index 4 项目3,索引4

ListBox: 列表框:

  • item1 项目1
  • item2 item2
  • empty space 空的空间
  • item3 项目3

the list can contain any number of combination of empty or filled positions. 该列表可以包含空位置或空位置的任意数量的组合。

Which methods would be the most direct way of achieving the desired result with MVVM structure in a WPF environment? 在WPF环境中,哪种方法是使用MVVM结构获得所需结果的最直接方法?

Doing this MVVMfully, your list box items will be an ObservableCollection of instances of some class of your own, which represents whatever you're putting in the listbox (users, used cars, whatever). 完整地执行MVVM,您的列表框项将是您自己某个类的实例的ObservableCollection ,它表示您要放入列表框的任何内容(用户,二手车等)。 That collection is a property on your viewmodel. 该集合是视图模型上的一个属性。

At the ListBox end of things, you'll have to represent empty slots with spacer items -- null should do fine. ListBox最后,您必须用空格项目代表空的插槽null应该可以。 You'd then do something in the view with the ListBox's ItemTemplate and ItemContainerStyle to display the "empty" items correctly and prevent them from being selected by the user. 然后,您可以在视图中使用ListBox的ItemTemplateItemContainerStyle进行某些操作,以正确显示“空”项目并防止用户选择它们。 You might have to handle the SelectionChanged event to make that happen; 您可能必须处理SelectionChanged事件才能实现此目的; if you want to reuse this stuff in a generalized way, you could handle SelectionChanged via an attached property that your style would apply. 如果您想以通用的方式重用这些东西,则可以通过您的样式将应用的附加属性来处理SelectionChanged

Option 1, not quite the purest MVVM but a whole lot less work than Doing It Right, is very simple: When you populate your ObservableCollection , sort the items and insert nulls for spacer items. 选项1并不是最纯粹的MVVM,但工作却比正确做起来要简单得多:当您填充ObservableCollection ,对项目进行排序并为间隔项目插入null。 Done. 做完了

<ListBox 
    ItemsSource="{Binding WeirdGappyCollectionWithNulls}" 
    ItemTemplate="{StaticResource GappyItemTemplate}"
    ItemContainerStyle="{StaticResource GappyItemContainerStyle}"
    />

...where GappyItemTemplate and GappyItemContainerStyle are defined in a ResourceDictionary above. ...其中GappyItemTemplateGappyItemContainerStyle在上面的ResourceDictionary中定义。

Option 2, if you want to go Full MVVMtard and build for the ages: 选项2,如果您想使用Full MVVMtard并针对以下年龄段进行构建:

For sorting, I'd ordinarily use a sorted CollectionViewSource . 为了进行排序,我通常使用排序后的CollectionViewSource Then you're binding that to ItemsSource 然后将其绑定到ItemsSource

<ListBox 
    ItemTemplate="{StaticResource GappyItemTemplate}"
    ItemContainerStyle="{StaticResource GappyItemContainerStyle}"
    >
    <ListBox.ItemsSource>
        <local:GappyCollectionViewSource
            Source="{Binding GappyCollection}"
            ...etc. etc.
            />
    </ListBox.ItemsSource>
</ListBox>

I wouldn't want the viewmodel to have any clue about ListBoxes, or even about empty items. 我不希望viewmodel对ListBoxes甚至空项目有任何线索。

I would give the viewmodel a gappy collection of "non empty" items, and write a CollectionViewSource subclass that will sort by whatever property you like, and additionally project that collection to another, readonly collection which includes null "spacer" items, which it exposes in its View property. 我将给viewmodel一个空的“非空”项目集合,并编写一个CollectionViewSource子类,该子类将按您喜欢的任何属性进行排序,并将该集合投影到另一个包含空“ spacer”项目的只读集合中,该集合将其公开在其View属性中。

Sort of an anti-filter. 有点像反过滤器。 It could use the SortDescriptions to define what the gaps are, at least if you're sorting by integers. 至少在按整数排序时,它可以使用SortDescriptions定义差距。 Or you could do that part semi-quick and dirty by giving it a keyselector lambda or an event analogous to the existing Filter event . 或者,您可以通过给它一个keyselector lambda或一个类似于现有Filter事件的事件来半快而肮脏。

CollectionViewSource.View isn't a virtual property, but it is a DependencyProperty . CollectionViewSource.View不是虚拟属性,但它是DependencyProperty There should be some way to slip in a ringer there. 应该有一些方法可以让铃声响起。 If that gives you any trouble, start over with CollectionView instead. 如果那给您带来麻烦,请改用CollectionView重新开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM