-1

I am exceptionally confused about how ObservableCollection and CollectionViews should be used in an MVVM application. I am NOT interested in technical explanations where you tell me how 'you're actually using CollectionView when you bind to ObservableCollection'.

I have a ListBox. The ListBox is databound to a ObservableCollection collection.

I also have a ComboBox. In the ComboBox, I have two strings: "Sort Alphabetically" and "Sort Reverse-Alphabetically". Depending on which item is selected, the logical sort action occurs. Nothing special here at all.

Now, I still have requirements. There must be NO visual side effects when the sort occurs.

For example:

  1. The user see "m", "n", "o" in the ListBox, he selects "n", then he sorts reverse-alphabetically.
  2. The user now sees "o", "n", "m". The ListBox DOES NOT scroll in any way. The user should still see "n" being selected(the SelectedItem is bound to the ViewModel and no change events should have fired).

Is what I want achievable? There is no code because I honestly don't know if CollectionView is meant to support this.

If you can just point me to a tutorial about this, that would also help(preferably one with complete code). I honestly couldn't find a tutorial about something as simple as dynamically sorting a listbox.

FZdev
  • 418
  • 1
  • 5
  • 10
  • In summary, no technical explanations, you want to know how to CollectionView because you could not find a tutorial, and you are not sure if CollectionView is the right tool. From MSDN search on "CollectionView WPF" and first item has a sample on sorting a ListBox. http://msdn.microsoft.com/en-us/library/ms754073.aspx – paparazzo Mar 10 '12 at 14:43
  • He's asking how to sort while keeping the currently selected item in the same place. – Phil Mar 10 '12 at 15:16
  • @Phil "couldn't find a tutorial about something as simple as dynamically sorting a listbox" It was posted as a comment not a an answer. – paparazzo Mar 10 '12 at 20:12
  • yes....apparently searching for 'WPF CollectionView' is totally different than 'CollectionView WPF'. Sometimes, neither Google or I are as smart as we should be. – FZdev Mar 11 '12 at 00:21
  • For Microsoft development I start with msdn.microsoft.com and "library". – paparazzo Mar 11 '12 at 13:59

1 Answers1

0

This keeps selected item selected and visible but not necessarily in the same position. If selected item started with 12 items under it then sorted to second to bottom there are not 12 items put put under it.

<DockPanel>
    <Button Click="OnClick" Width="30" Height="20" DockPanel.Dock="Top" HorizontalAlignment="Left">Sort</Button>
    <ListBox Name="myListBox" DockPanel.Dock="Top" ItemsSource="{Binding Path=MyListBoxCollection}" 
             SelectedItem="{Binding Path=MyListBoxSelectedItem, Mode=TwoWay}" >
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Style.Resources>
                    <!-- This is the color used if the item is selected and the listbox has focus -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
                    <!-- Background of selected item when not focussed -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGreen" />
                </Style.Resources>
            </Style>
        </ListBox.Resources>
    </ListBox>
</DockPanel>


public partial class MainWindow : Window, INotifyPropertyChanged
{
    private List<ListBoxItem> myListBoxCollection = new List<ListBoxItem>();
    // private string myListBoxSelectedItem;

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public MainWindow()
    {
        ListBoxItem li;
        li= new ListBoxItem();
        li.Content = "delta0";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "gamma0";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "beta0";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "alpha0";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "delta1";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "gamma1";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "beta1";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "alpha1";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "delta2";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "gamma2";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "beta2";
        myListBoxCollection.Add(li);
        li = new ListBoxItem();
        li.Content = "alpha2";
        myListBoxCollection.Add(li);

        DataContext = this;

        InitializeComponent();
    }

    public List<ListBoxItem> MyListBoxCollection { get { return myListBoxCollection; } }

    public ListBoxItem MyListBoxSelectedItem { get; set; }

    private void OnClick(object sender, RoutedEventArgs e)
    {
        myListBox.Items.SortDescriptions.Add(new SortDescription("Content", ListSortDirection.Ascending));
        myListBox.ScrollIntoView(myListBox.SelectedItem);
    }
paparazzo
  • 44,497
  • 23
  • 105
  • 176