Search code examples
wpfxamlribbon

How to enable ScrollViewer for RibbonComboBox?


Using the following code, the dropdown-list of fonts runs off the screen. Setting ScrollViewer does not show scrollbars as I expected. A secondary issue is that I can mouse-move in the dropdown, but not mouse-scroll with mousewheel.

<RibbonComboBox 
    xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    ItemTemplate="{DynamicResource FontTemplate}"
    ScrollViewer.VerticalScrollBarVisibility="Visible"
    ScrollViewer.HorizontalScrollBarVisibility="Visible">

    <RibbonComboBox.Resources>
        <CollectionViewSource x:Key="myFonts" 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">
            <StackPanel VirtualizingStackPanel.IsVirtualizing="False">
                <TextBlock Style="{StaticResource FontStyle}"
                   Text="{Binding Source}"
                   ToolTip="{Binding Source}" />
            </StackPanel>
        </DataTemplate>
    </RibbonComboBox.Resources>

    <RibbonComboBox.ItemsSource>
        <Binding Source="{StaticResource myFonts}"/>
    </RibbonComboBox.ItemsSource>

</RibbonComboBox>

Solution

  • Answering my own question: this seems to do what I want, more or less.

    <RibbonComboBox xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase">
        <RibbonComboBox.Resources>
            <CollectionViewSource x:Key="myFonts" 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">
                <RibbonGalleryItem>
                    <TextBlock Style="{StaticResource FontStyle}"
                       Text="{Binding Source}"
                       ToolTip="{Binding Source}"/>
                </RibbonGalleryItem>
            </DataTemplate>
            <DataTemplate x:Key="myFontTemplate">
                <RibbonGalleryItem>
                    <TextBlock Style="{StaticResource FontStyle}"
                       Text="{Binding Source}"
                       ToolTip="{Binding Source}" />
                </RibbonGalleryItem>
            </DataTemplate>
        </RibbonComboBox.Resources>
        <RibbonGallery Name="RibbonGallery" MaxColumnCount="1">
            <RibbonGalleryCategory ItemsSource="{Binding Source={StaticResource myFonts}}"
                       ItemTemplate="{DynamicResource myFontTemplate}"
                       ScrollViewer.VerticalScrollBarVisibility="Auto"
                       ScrollViewer.CanContentScroll="True"/>
        </RibbonGallery>
    </RibbonComboBox>