我需要在wpf中按listboxitem的sSelected属性对所有项进行排序。我怎么能这样做?
答案 0 :(得分:0)
目前还不清楚你的物品是什么,所以我会使用整数。想法是用户将选择项目,按下按钮将启动排序,并且所选项目将被排序,放置在列表的开头,然后将在新排序的项目之后添加未选择的项目。 / p>
的Xaml:
<StackPanel Orientation="Vertical">
<ListBox Name="lbData" SelectionMode="Multiple"/>
<Button Click="Button_Click" Height="20"/>
</StackPanel>
C#代码背后
public MainWindow()
{
InitializeComponent();
lbData.ItemsSource = new List<int> {1, 7, 2, 4, 10};
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var selected = lbData.SelectedItems
.OfType<int>()
.ToList();
if (selected.Any())
{
// Remove the items
var diff = lbData.ItemsSource
.OfType<int>()
.Except(selected);
// Sort the selected and place them at the beginning
var ordered = selected.OrderBy(itm => itm).ToList();
ordered.AddRange(diff);
lbData.ItemsSource = ordered;
}
}