0

I try to modify selected item by code of a RibbonComboBox:

<r:RibbonComboBox x:Name="RibbonComboxBoxEditEnemyProjectiles" Label="Projectile"   SmallImageSource="img/history16.png">
    <r:RibbonGallery SelectedItem="{Binding Path=iSpriteIdx}" >
        <r:RibbonGalleryCategory ItemsSource="{Binding oProjectiles, Source={StaticResource GameInfos}}" />
    </r:RibbonGallery>
</r:RibbonComboBox>

I try 2 ways :

RibbonGalleryEditProjectileSprite.SelectedItem  = GameData.oSprites[ idx ];
RibbonGalleryEditProjectileSprite.SelectedValue = GameData.oSprites[ idx ];

This ways don't work. What is a correct way to do that ?

Thanks

H.B.
  • 166,899
  • 29
  • 327
  • 400
chkone
  • 3
  • 1
  • 3
  • What do you mean by "This ways don't work" ? Please provide more details e.g. if control is not updated or it throw any exception. – Nitesh Jul 21 '13 at 04:14
  • Sorry for the answer time. "Don't work" == "Control is not updated & no exception – chkone Aug 05 '13 at 02:24

1 Answers1

1

If you search this website before you post questions, you can find good answers... Take a look at this post and this post.

UPDATE >>>

The How to set SelectedItem on a RibbonComboBox using MVVM? post that I gave you a link for has a complete solution for you. Basically the answer is this:

Whatever object that you set as the RibbonGallery.DataContext should have a collection property to bind to the RibbonGalleryCategory.ItemsSource property and a property of the same type as the collection items to bind to the RibbonGallery SelectedItem property.

Let's say that your selected item property is called SelectedItem. You will then be able to set the SelectedItem property of the object that you set as the RibbonGallery.DataContext to an item from the collection and the binding will change the selected item in the RibbonGallery UI.

Note that if you are using objects as your gallery data types (as opposed to primitives like string, etc.), then this will only work if you set your SelectedItem property to an actual item from the collection and not just one with the same values. This can be easily achieved if your data object has a property with unique values by the following:

DataContextData.SelectedItem = DataContextData.CollectionData.Where(d => d.Id == 
itemToSelect.Id).Single();

If that still hasn't helped, I did a quick search online and found a complete solution that you can download and examine at your leisure in the How do I add Galleries to my Ribbon? post at 'The official blog of the Windows Presentation Foundation Team'. It shows a good example of the method that I just described.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Any body have any idea to do that? I do not find a way to modify RibbonComboxBox selectedItem/selectedValue by code... – chkone Aug 18 '13 at 00:36
  • Thanks, I search on stackoverflow but apparently not with a correct keyword. – chkone Sep 03 '13 at 14:16
  • If your question has been answered, please mark it as so by clicking on the green tick, so other users know. – Sheridan Sep 03 '13 at 14:18