ScrollBar Windows 应用商店

ScrollBar Windows Store App

我正在尝试创建自己的控件。我坚持要给它添加滚动条。

出于测试目的,我对滚动条值进行了硬编码。

这是我的 class:

public sealed class BarrelPanel : Control
    {
        public static readonly DependencyProperty VerticalScrollBarVisibilityProperty =
        DependencyProperty.Register(
        "VerticalScrollBarVisibility", typeof(Visibility),
        typeof(BarrelPanel), null);

        public Visibility VerticalScrollBarVisibility
        {
            get { return (Visibility)GetValue(VerticalScrollBarVisibilityProperty); }
            set { SetValue(VerticalScrollBarVisibilityProperty, value); }
        }

        public BarrelPanel()
        {
            this.DefaultStyleKey = typeof(BarrelPanel);
        }
    }

这是我的风格:

<Style TargetType="local:BarrelPanel">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:BarrelPanel">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Background="{TemplateBinding Background}"/>
                        <ScrollBar Grid.Column="1" Maximum="10" Orientation="Vertical" Visibility="{TemplateBinding VerticalScrollBarVisibility}" ViewportSize="1"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

当我将可见性 属性 设置为启用时,滚动条占据了网格的一部分:

<local:BarrelPanel HorizontalAlignment="Left" Background="White" Margin="722,212,0,0" VerticalScrollBarVisibility="Visible" VerticalAlignment="Top" Height="217" Width="150"/>

示例:

但它没有显示可拖动的矩形或导航按钮 - 为什么?

我找到了答案,更多调查 msdn:

https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.primitives.scrollbar.aspx

我的滚动条没有显示出来,因为你必须指定要显示的滚动条类型:

IndicatorMode="MouseIndicator"

IndicatorMode="TouchIndicator"

默认为none。