This project is read-only.

How to display selected item of ComboBox

May 30, 2014 at 6:07 PM

when I select a new item of the Fluent.ComboBox.Items by using SelectedIndex or SelectedItem, the item is selected but not shown in the edit field of the ComboBox.

When I open the ComboBox it is the first element beneath and it is selected, even if it is, like in my test case, element with index 118.

What I've to do to display the selected item in the box? I already tried many things but it seems all the wrong ones.

The XAML part:
<Fluent:ComboBox xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
          ItemTemplate="{DynamicResource FontTemplate}">
        <CollectionViewSource x:Key="FontSelectorBoxFonts"
                              Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
                <ComponentModel:SortDescription PropertyName="Source" />
        <Style x:Key="FontStyle">
            <Setter Property="Control.FontFamily" Value="{Binding Source}" />
            <Setter Property="Control.FontSize" Value="16" />
        <DataTemplate x:Key="FontTemplate">
            <TextBlock Style="{StaticResource FontStyle}"
                        Text="{Binding Source}"
                        ToolTip="{Binding Source}" />
        <Binding Source="{StaticResource FontSelectorBoxFonts}" />
The code behind of this class:
private void TextEditor_OnDocumentLoaded(object flowDocument, RoutedEventArgs routedEventArgs)
    if (currentCursorPosition == null)
        currentCursorPosition = TextEditor.EditorTextBox.CaretPosition;
        currentCursorPosition = currentCursorPosition.DocumentStart;
        //currentCursorPosition = currentCursorPosition.DocumentEnd;
        TextEditor.EditorTextBox.CaretPosition = currentCursorPosition;
    TextEditor.EditorTextBox.CaretPosition = currentCursorPosition.DocumentStart;
    TextEditor.EditorTextBox.Document.FontSize = 14.0;
    var nextCharacters = new TextRange(currentCursorPosition, currentCursorPosition);
    var fontFamily = (FontFamily)nextCharacters.GetPropertyValue(TextElement.FontFamilyProperty);
    FontSelectorBox.SelectedIndex = FontSelectorBox.Items.IndexOf(fontFamily);
    FontSelectorBox.SelectedItem = FontSelectorBox.Items.GetItemAt(FontSelectorBox.SelectedIndex);
Asking greetings

May 30, 2014 at 7:12 PM
There is nothing wrong with the Ribbon. It just behaves like a standard ComboBox, where you have to use the DisplayMemberPath, as far as i can remember. ;-)
Marked as answer by batzen on 9/20/2014 at 4:21 AM
May 31, 2014 at 8:23 AM
Edited May 31, 2014 at 9:11 AM
Of course I tried it with a test solution with the normal ComboBox and there it is no necessary to do it this way. It is right brought into view.

EDIT: Made a new test with simply the name (attribute Source) of the FontFamily in another test solution and the ComboBox behaves like it should. Though, there must be a problem with the template. Perhaps just something is missing...
Jun 2, 2014 at 10:48 AM
Now I'm really confused! In the Fluent.Sample.Foundation I added a Fluent.RibbonGroupBox with a Fluent.ComboBox inside. I added the XAML code shown above and the following into the code behind C# file.
public Window()
    var fontFamily = new FontFamily("Goudy-Elegant");
    var index = FontSelectorBox.Items.IndexOf(fontFamily);
    FontSelectorBox.SelectedIndex = index;
    FontSelectorBox.SelectedValue = fontFamily;
    FontSelectorBox.SelectedValuePath = "Source";
This worked fine and therefore I added it into the original project. But again, over here the selected value wouldn't show. :-(

Instead when debugging and pressing F10 at the line "FontSelectorBox.SelectedIndex = index;" the debugger switched, in opposite to the test/sample project, to the disassembly into "Fluent.RibbonWindow.UpdateFrameState(bool)".

Is there a setting/attribute that could cause this different behavior?
Jun 2, 2014 at 4:04 PM
Edited Jun 2, 2014 at 4:04 PM
I am absolutely annoyed! I hate multithreading!

It's curios but in another event handler that must be called always at the same moment, was a test for null missing and an exception occured. But it was nowhere to see, or better not easily to see, even not in the call stack. I just found it because I removed some of the other tab elements of the ribbon and then the time schedule was different and the debugger stopped at this exception before the handler for the ComboBox was reached.