首页 > 解决方案 > 如何为 RibbonComboBox 启用 ScrollViewer?

问题描述

使用以下代码,字体的下拉列表会在屏幕外运行。设置ScrollViewer没有像我预期的那样显示滚动条。第二个问题是我可以在下拉菜单中移动鼠标,但不能使用鼠标滚轮进行鼠标滚动。

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

标签: wpfxamlribbon

解决方案


回答我自己的问题:这似乎或多或少地做了我想要的。

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

推荐阅读