PeterP
PeterP

Reputation: 771

Changing display (UI) / order of items Observable Collection

EDIT #2:

    public void ReverseOrderRegBtns()
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(GlobalObservableCol.regBtns);
        view.SortDescriptions.Add(new SortDescription("ReverseOrder", ListSortDirection.Ascending));
        view.Refresh();
    }

Not really sure how to fit the ICollectionView in, it also interrupts timers that are doing some logic. For some reason it interupts my timing that starts counting when a button gets created.

My application has several timers, so the buttons that get created get updates with possible new content every x amount of seconds.

EDIT:

The databinding looks as following:

<ListBox x:Name="lbRegistration" ItemsSource="{Binding regBtnsReverse, ElementName=Window}" Background="{x:Null}" 

Old: public ObservableCollection RegBtns { get { if (GlobalObservableCol.regBtns == null) { return new ObservableCollection(); } return GlobalObservableCol.regBtns; } }

New (no longer works), the reverse is in the ObserableCol class

public IEnumerable<RegistrationButton> RegBtns
        {
            get
            {
                return GlobalObservableCol.regBtnsReverse;
            }
        }

I have created the IEnumerable in my a static class where the ObservableCollection list is available.

I cannot seem to bind when my databinding is a IENumerable. How to bind the NumbersReverse method that is located in a other class to my MainWindow.xls?


I have a ObservableCollection that holds custom styled WPF buttons.

Those items get dynamically created and added on a custom slider control (listbox). For example Button 1, Button 2, Button 3, ...

What i want is exactly the opposite.

For example Button 3, Button 2, Button 1. This sounds like a linked list but my entire application is already using this Observable Collection throughout the entire logic.

When the buttons get displayed in the UI the last added button should be placed as the first item on my slider (that is a listbox control).

How exactly can i pull this off?

Thanks, PeterP

Upvotes: 3

Views: 2432

Answers (3)

Jaster
Jaster

Reputation: 8581

<ListBox ItemsSource="{Binding Foo}"/>

private ObservableCollection<RegButtton> RegBtns = new...;

public ICollectionView Foo { get; set; }

Foo = CollectionViewSource.GetDefaultView(RegBtns);
Foo.SortDescriptions.Add(new SortDescription("SomePropertyName", ListSortDirection.Ascending));
Foo.Refresh();

Upvotes: 3

Michal B.
Michal B.

Reputation: 5719

I do not know what framework you use. I would do something like in my view model:

public ObservableCollection<int> Numbers { get; private set; }
public IEnumerable<int> NumbersReverse
{
    get
    {
        return Numbers.Reverse();
    }

}

and I would use NumbersReverse as a datasource:

<ListBox Name="listBox1" ItemsSource="{Binding NumbersReverse}"/>

Of course there are many other ways of doing that. You have to figure out what's best in your case...

Edit: e.g. if you do not care about the order of the items for any other purpose, then the solution provided by stijn is much better.

Upvotes: 1

stijn
stijn

Reputation: 35901

Somewhere in your code there should be myCollection.Add( someButton ).

Replace it with myCollection.Insert( 0, myButton ) and you're finished.

Upvotes: 2

Related Questions