Click here to Skip to main content
15,887,485 members
Articles / Desktop Programming / WPF
Tip/Trick

ComboBox ObservableCollection Overkill

Rate me:
Please Sign up or sign in to vote.
4.92/5 (5 votes)
6 Dec 2013CPOL1 min read 18.7K   4   5
RibbonComboBox in MVVM/WPF: the easiest way

Introduction

When you implement a RibbonComboBox in .NET 4.5, WPF (or a ComboBox in general) and you want to apply MVVM, you find tons of examples using an ObservableCollection for this. But in how many cases do the values selectable in your (Ribbon)ComboBox really change during runtime of the program? If they do not, take it easy: a simple array instead of the ObservableCollection is enough.

Using the Code

Look at this simple property in your ViewModel:

C#
public int[] SelectableValues { get { return new int[] { 20, 30, 40, 45, 48 }; } }

This is the corresponding XAML:

XML
<RibbonComboBox SelectionBoxWidth="30" 
IsEditable="False" IsReadOnly="False">
    <RibbonGallery SelectedValue="{Binding ActualValue}">
        <RibbonGalleryCategory ItemsSource="{Binding SelectableValues}" />
    </RibbonGallery>
</RibbonComboBox> 

And here we have the actually selected value (again in the ViewModel, I suggest):

C#
private int _actualValue;
public int ActualValue
{
   get { return _actualValue; }
   set
   {
      if (_actualValue == value) return;
      _ actualValue = value;
      RaisePropertyChanged("ActualValue");
   }
}

That's it. No ObservableCollection. Because there is nothing to observe. Only the selected value changes, but the collection of values itself does not change - if it really does not change, of course.

Points of Interest

The internet is full of examples addressing this topic. But I think these examples are too complicated when the list of selectable values does not change.

You can also use string[] and even MyClass[] as array for fixed values (instead of the ObservableCollection). In case of a class, simply override its ToString()-method and there return the specific property of your class you want to see in the (Ribbon)ComboBox.

History

  • 6.12.2013: First posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect www.schoder.uk
United Kingdom United Kingdom
I am a software architect and electronic musician.

Comments and Discussions

 
QuestionThank you! Pin
coderprime1-Jun-15 23:12
coderprime1-Jun-15 23:12 
QuestionURI is not working Pin
Sampath Lokuge6-Dec-13 20:07
Sampath Lokuge6-Dec-13 20:07 
AnswerRe: URI is not working Pin
dietmar paul schoder6-Dec-13 20:14
professionaldietmar paul schoder6-Dec-13 20:14 
GeneralRe: URI is not working Pin
Sampath Lokuge6-Dec-13 20:23
Sampath Lokuge6-Dec-13 20:23 
GeneralRe: URI is not working Pin
dietmar paul schoder6-Dec-13 20:27
professionaldietmar paul schoder6-Dec-13 20:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.