Click here to Skip to main content
12,253,359 members (53,847 online)
Click here to Skip to main content
Add your own
alternative version

Stats

9.9K views
5 bookmarked
Posted

ComboBox ObservableCollection Overkill

, 6 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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

This is the corresponding XAML:

<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):

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)

Share

About the Author

dietmar paul schoder
Architect http://www.schoder.uk
United Kingdom United Kingdom
I am a developer and convinced user of Visual Studio, C#, .net, WPF, MVVM, the project management standards of PMI and IPMA - and Microsoft Surface Pro 3. I live in Eastbourne, United Kingdom.

You may also be interested in...

Comments and Discussions

 
QuestionThank you! Pin
coderprime1-Jun-15 23:12
membercoderprime1-Jun-15 23:12 
QuestionURI is not working Pin
Sampath Lokuge6-Dec-13 20:07
memberSampath Lokuge6-Dec-13 20:07 
AnswerRe: URI is not working Pin
Dietmar Schoder6-Dec-13 20:14
professionalDietmar Schoder6-Dec-13 20:14 
GeneralRe: URI is not working Pin
Sampath Lokuge6-Dec-13 20:23
memberSampath Lokuge6-Dec-13 20:23 
GeneralRe: URI is not working Pin
Dietmar Schoder6-Dec-13 20:27
professionalDietmar 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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 7 Dec 2013
Article Copyright 2013 by dietmar paul schoder
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid