简体   繁体   中英

WPF order listbox items

How do I order my items bound from model to the list box.

I have defined model that is:

public ObservableCollection<NotificationItem> Classes:

I need to order it by id which is assigned to every notification item.

at present I have definition:

  <ListBox ItemsSource="{Binding Classes, Source={StaticResource model}}"
                   ScrollViewer.VerticalScrollBarVisibility="Visible"
DataContext="{Binding}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Label Content="{Binding Name}" />
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

where I have reference of model:

<Model:ClassModel x:Key="model" />

Update:

where initialisation of the model is done by

 try
            {
                this.notifierModel = this.Resources["model"] as ClassModel;

                this.classController.Initialize(this.notifierModel);

            }
            catch
            {
             // todo: handle exception
            }

您可以直接在模型(数据上下文)上订购集合,也可以创建自定义CollectionViewSource并将ListBox绑定到该集合

Where you set up Classes you need to have:

Classes = new ObservableCollection(results.OrderBy(i => i.Id));

instead of just:

Classes = new ObservableCollection(results);

assuming that results is list of data returned by your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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