【问题标题】:In WPF Disable Scrollbar RepeatButtons when no data at their ends在 WPF 中,当末端没有数据时禁用滚动条重复按钮
【发布时间】:2012-09-18 07:37:37
【问题描述】:

我有一个滚动查看器控件。它的编码

<Window.Resources>        
    <DataTemplate x:Key="listBoxItemTemplate">
        <TextBlock />
    </DataTemplate>
    <ItemsPanelTemplate x:Key="itemsPanelTemplate">
        <VirtualizingStackPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="0"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <RepeatButton x:Name="LineLeftButton" 
                Grid.Column="0"
                Grid.Row="1"
                Content="&lt;"      
                Command="{x:Static ScrollBar.LineLeftCommand}"      
                CommandTarget="{Binding ElementName=scrollViewer}"/>
    <RepeatButton x:Name="LineRightButton" 
                Grid.Column="2"
                Grid.Row="1"
                Content="&gt;" 
                Command="{x:Static ScrollBar.LineRightCommand}"      
                CommandTarget="{Binding ElementName=scrollViewer}"/>
    <ScrollViewer Grid.Column="1" Grid.Row="1"  x:Name="scrollViewer" 
                  VerticalScrollBarVisibility="Hidden" 
                  HorizontalScrollBarVisibility="Hidden">
        <ListBox Name="lst2"
                 Margin="0,0,0,0"
                 VerticalAlignment="Stretch" 
                 ItemsPanel="{StaticResource itemsPanelTemplate}"/>
    </ScrollViewer>
</Grid>

我想在没有数据时禁用重复按钮。

也就是说,当我滚动列表框数据时,最后当特定一侧(即左、右)没有可用数据时,该侧的重复按钮将被禁用。当我向后滚动时,将启用上述重复按钮。

我在这里展示了一个图形表示。我相信这可以正确澄清。

图片1:

请检查左侧的重复按钮是否已禁用,因为左侧没有可滚动的数据。

图片2:

请检查右侧的重复按钮是否已禁用,因为右侧没有可滚动的数据。

这种类型的滚动是我想要实现的。我读了Wpf disable repeatbuttons when scrolled to top/bottom 但没用。

【问题讨论】:

    标签: c# wpf vb.net repeatbutton


    【解决方案1】:

    是的,这很容易。

    • 摆脱您的 ScrollViewer
    • 子类 ListBox 组件并添加新属性 CanScrollHorizo​​ntallyLeft/Right
    • 挂钩到 ListBox 的 ScrollBar.Scroll 事件,例如:&lt;ListBox ScrollBar.Scroll="event_handler" /&gt;
    • 添加检测并相应更改属性。

      private void scroll_handler(object sender, ScrollEventArgs e) {
         ScrollBar sb = e.OriginalSource as ScrollBar;
      
         if (sb.Orientation == Orientation.Horizontal)
             return;
      
         if (sb.Value == sb.Maximum) {
             Debug.Print("At the bottom of the list!");
      }
      

      }

    另外,ScrollViewer 也可能暴露 ScrollBar.Scroll 事件,您不必继承/创建新属性。您可以在 scroll_handler 中执行逻辑并更改命令 CanExecute。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 2012-10-07
      • 2015-01-02
      • 1970-01-01
      相关资源
      最近更新 更多