質問

I have a scrollviewer control. Its coding

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

enter image description here

I want to disable repeat buttons when there is no data at that end.

That is to say when I am scrolling my Listbox data and at the end when no data is available at that particular side (i.e., left, right) then that side RepeatButton will be disabled. When I am scrolling reverse then the said RepeatButton will be enabled.

I am showing a graphical representation here. I am sure that can clarify properly.

Image1:

enter image description here

Please check the RepeatButton is disabled at the left side as no data to be scrolled at left.

Image2:

enter image description here

Please check the RepeatButton is disabled at the right side now as no data to be scrolled at right.

This type of scrolling is what I am trying to achieve. I read Wpf disable repeatbuttons when scrolled to top/bottom but of no use.

役に立ちましたか?

解決

Yes, it is easy.

  • Get rid of your ScrollViewer
  • Subclass ListBox component and add new property CanScrollHorizontallyLeft/Right
  • Hook into ListBox's ScrollBar.Scroll event, like : <ListBox ScrollBar.Scroll="event_handler" />
  • Add the detection and change properties accordingly.

    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!");
    }
    

    }

Alternatiely, ScrollViewer might expose ScrollBar.Scroll event too and you dont have to subclass/make new properties. YOu can do the logic inside scroll_handler and change commands CanExecute.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top