【问题标题】:How to enable ScrollViewer for RibbonComboBox?如何为 RibbonComboBox 启用 ScrollViewer?
【发布时间】:2019-04-06 00:45:05
【问题描述】:

使用以下代码,字体的下拉列表会从屏幕上消失。设置 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>

【问题讨论】:

    标签: wpf xaml ribbon


    【解决方案1】:

    回答我自己的问题:这似乎或多或少符合我的要求。

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

    【讨论】:

    • 使用此代码,下拉菜单以自己的风格显示字体,我可以使用鼠标滚动列表。但是,选定的项目(也称为 DropDownClosed 之后)将保留样式,使其看起来很尴尬。还没弄清楚如何操纵它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多