none
ScrollViewer滚动条显示/隐藏 RRS feed

  • 问题

  • 为了让WPF运行在可触摸的平板上,对滚动条进行显示或隐藏。

    桌面:当鼠标经过时,设置滚动条为Auto,

    平板:设置为Disabled

    <ScrollViewer x:Name="scrollViewer" HorizontalAlignment="Stretch"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <StackPanel x:Name="BottomButtonRoot" Orientation="Horizontal"  Height="50"
                HorizontalAlignment="Stretch" VerticalAlignment="Center">
            <Button x:Name="button01" Content="Button 01" Height="40" Width="150"
                HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
            <Button x:Name="button02" Content="Button 02" Height="40"
                HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
            <Button x:Name="button03" Content="Button 03" Height="40"
                HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
            <Button x:Name="button04" Content="Button 04" Height="40"
                HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
            <Button Content="Button 05" Height="40"
                HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
            <Button Content="Button 06" Height="40"
                HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
            <Button Content="Button 07" Height="40"
                HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
            <Button Content="Button 08" Height="40"
                HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
        </StackPanel>
    
    
        <ScrollViewer.Triggers>
                
        </ScrollViewer.Triggers>
    </ScrollViewer>

    不是在Style里面,是在界面布局里面,咋实现?



    • 已编辑 oneonce 2017年8月23日 2:54
    2017年8月23日 2:49

答案

  • 你好,

    你可以尝试使用数据触发器来处理。

      public bool IsTabletMode
            {
                get { return (bool)GetValue(IsTabletModeProperty); }
                set { SetValue(IsTabletModeProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for IsTabletMode.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty IsTabletModeProperty =
                DependencyProperty.Register("IsTabletMode", typeof(bool), typeof(sample1), new PropertyMetadata(true))

         <ScrollViewer HorizontalAlignment="Stretch">
                <ScrollViewer.Style>
                    <Style TargetType="{x:Type ScrollViewer}">
                        <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
                        <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>                    
                        <Style.Triggers>                       
                            <DataTrigger Binding="{Binding IsTabletMode}" Value="true">
                                <Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/>
                                <Setter Property="VerticalScrollBarVisibility" Value="Hidden"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ScrollViewer.Style>
              
            </ScrollViewer>

    >>桌面:当鼠标经过时,设置滚动条为Auto,

    当鼠标经过谁的时候将滚动条设置为Auto,如果你是指ScrollViewer的边缘时,那你有必要在MouseMove进行判断处理了。

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 oneonce 2017年8月23日 7:20
    2017年8月23日 5:46
    版主