How to display selected item of ComboBox

May 30, 2014 at 5:07 PM
Hi,

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"
          x:Name="FontSelectorBox"
                 MinWidth="150"
          Margin="5"
          VerticalAlignment="Center"
                 HorizontalAlignment="Left"
          ItemTemplate="{DynamicResource FontTemplate}">
    <Fluent:ComboBox.Resources>
        <CollectionViewSource x:Key="FontSelectorBoxFonts"
                              Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
            <CollectionViewSource.SortDescriptions>
                <ComponentModel:SortDescription PropertyName="Source" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
        <Style x:Key="FontStyle">
            <Setter Property="Control.FontFamily" Value="{Binding Source}" />
            <Setter Property="Control.FontSize" Value="16" />
        </Style>
        <DataTemplate x:Key="FontTemplate">
            <TextBlock Style="{StaticResource FontStyle}"
                        Text="{Binding Source}"
                        ToolTip="{Binding Source}" />
        </DataTemplate>
    </Fluent:ComboBox.Resources>
    <Fluent:ComboBox.ItemsSource>
        <Binding Source="{StaticResource FontSelectorBoxFonts}" />
    </Fluent:ComboBox.ItemsSource>
</Fluent:ComboBox>
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.BeginInit();
    FontSelectorBox.SelectedIndex = FontSelectorBox.Items.IndexOf(fontFamily);
    FontSelectorBox.SelectedItem = FontSelectorBox.Items.GetItemAt(FontSelectorBox.SelectedIndex);
    FontSelectorBox.EndInit();
...
}
Asking greetings

Reiner
Developer
May 30, 2014 at 6: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 7:23 AM
Edited May 31, 2014 at 8: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 9: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()
{
    InitializeComponent();
    FontSelectorBox.BeginInit();
    var fontFamily = new FontFamily("Goudy-Elegant");
    var index = FontSelectorBox.Items.IndexOf(fontFamily);
    FontSelectorBox.SelectedIndex = index;
    FontSelectorBox.SelectedValue = fontFamily;
    FontSelectorBox.SelectedValuePath = "Source";
    FontSelectorBox.EndInit();
}
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 3:04 PM
Edited Jun 2, 2014 at 3: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.