Wpf disables repeat buttons when scrolling up / down

I am creating a touch screen interface that uses a list.
I have a button above and below the list for the page up / down.

I try to find it where, when scrolling up, the button at the top of the page turns off.
and when scrolling to the end, the pagedown button also turns off.

Here is the code in my Styles.xaml for Listbox

<Style x:Key="{x:Type ListBox}" TargetType="{x:Type ListBox}">  
    <Setter Property="Template">  
        <Setter.Value>  
            <ControlTemplate x:Key="{x:Type ListBox}" TargetType="{x:Type ListBox}">  
                <DockPanel>  
                    <RepeatButton x:Name="LineUpButton" DockPanel.Dock="Top"  
                        HorizontalAlignment="Stretch"   
                        Height="50"  
                        Content="/\"  
                        Command="{x:Static ScrollBar.PageUpCommand}"  
                        CommandTarget="{Binding ElementName=scrollviewer}" />    
                    <RepeatButton x:Name="LineDownButton" DockPanel.Dock="Bottom"  
                        HorizontalAlignment="Stretch"  
                        Height="50"  
                        Content="\/"  
                        Command="{x:Static ScrollBar.PageDownCommand}"  
                        CommandTarget="{Binding ElementName=scrollviewer}" />  
                    <Border BorderThickness="1" BorderBrush="Gray" Background="White">    
                        <ScrollViewer x:Name="scrollviewer">  
                            <ItemsPresenter/>  
                        </ScrollViewer>  
                    </Border>  
                </DockPanel>  
            </ControlTemplate>  
        </Setter.Value>  
    </Setter>  
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>  
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>  
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />  
</Style> 

And here where I instantiate the list

<ListBox SelectedItem="{Binding SelectedCan}" ItemsSource="{Binding Path=SelectedKioskCashCans}">  
    <ListBox.ItemTemplate>  
        <DataTemplate>  
            <ContentPresenter Content="{Binding image}" MaxWidth="75" />  
        </DataTemplate>  
     </ListBox.ItemTemplate>  
     <ListBox.ItemsPanel>  
         <ItemsPanelTemplate>  
             <VirtualizingStackPanel Orientation="Vertical"/>  
         </ItemsPanelTemplate>  
     </ListBox.ItemsPanel>  
</ListBox> 

I searched all around yesterday, no luck.
I hope I can do it all on haml.

I use images for buttons, but took them out to read above,
they really look like ...

<RepeatButton x:Name="LineUpButton" DockPanel.Dock="Top" HorizontalAlignment="Stretch" 
    Height="50"      
    Command="{x:Static ScrollBar.PageUpCommand}"      
    CommandTarget="{Binding ElementName=scrollviewer}">
        <RepeatButton.Template>
             <ControlTemplate TargetType="{x:Type RepeatButton}">
                 <Grid>
                     <Image Name="Normal" Source="/Images/up.png"/>
                     <Image Name="Pressed" Source="/Images/up.png" Visibility="Hidden"/>
                 </Grid>
                 <ControlTemplate.Triggers>
                      <Trigger Property="IsPressed" Value="True">
                          <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
                          <Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
                      </Trigger>
                  </ControlTemplate.Triggers>
             </ControlTemplate>
        </RepeatButton.Template>
   </RepeatButton>
+3
source share
1 answer

CanExecute PageUpCommand . false, , .

EDIT:

, . ScrollViewer:

<ScrollViewer x:Name="scrollviewer"
              z:ScrollBarCommandsCanExecuteFixBehavior.IsEnabled="True">  
     <ItemsPresenter/>  
</ScrollViewer> 

:

public static class ScrollBarCommandsCanExecuteFixBehavior
{
    #region Nested Types

    public class CommandCanExecuteMonitor<T> where T : UIElement
    {
        protected T Target { get; private set; }

        protected CommandCanExecuteMonitor(T target, RoutedCommand command)
        {
            Target = target;

            var binding = new CommandBinding(command);

            binding.CanExecute += OnCanExecute;

            target.CommandBindings.Add(binding);
        }

        protected virtual void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {

        }
    }

    public class PageUpCanExecuteMonitor : CommandCanExecuteMonitor<ScrollViewer>
    {
        public PageUpCanExecuteMonitor(ScrollViewer scrollViewer)
            : base(scrollViewer, ScrollBar.PageUpCommand)
        {
        }

        protected override void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }

            if (Equals(Target.VerticalOffset, 0.0))
            {
                e.CanExecute = false;
                e.Handled = true;
            }
        }
    }

    public class PageDownCanExecuteMonitor : CommandCanExecuteMonitor<ScrollViewer>
    {
        public PageDownCanExecuteMonitor(ScrollViewer scrollViewer)
            : base(scrollViewer, ScrollBar.PageDownCommand)
        {
        }

        protected override void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }

            if (Equals(Target.VerticalOffset, Target.ScrollableHeight))
            {
                e.CanExecute = false;
                e.Handled = true;
            }
        }
    }

    #endregion

    #region IsEnabled Attached Property

    public static bool GetIsEnabled(DependencyObject obj)
    {
        return (bool) obj.GetValue(IsEnabledProperty);
    }

    public static void SetIsEnabled(DependencyObject obj, bool value)
    {
        obj.SetValue(IsEnabledProperty, value);
    }

    public static readonly DependencyProperty IsEnabledProperty =
        DependencyProperty.RegisterAttached("IsEnabled", typeof (bool), typeof (ScrollBarCommandsCanExecuteFixBehavior), new PropertyMetadata(false, OnIsEnabledChanged));

    private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if ((bool) e.NewValue)
        {
            var scrollViewer = d as ScrollViewer;

            if (scrollViewer != null)
            {
                OnAttached(scrollViewer);
            }
            else
            {
                throw new NotSupportedException("This behavior only supports ScrollViewer instances.");
            }
        }
    }

    private static void OnAttached(ScrollViewer target)
    {
        SetPageUpCanExecuteMonitor(target, new PageUpCanExecuteMonitor(target));
        SetPageDownCanExecuteMonitor(target, new PageDownCanExecuteMonitor(target));
    }

    #endregion

    #region PageUpCanExecuteMonitor Attached Property

    private static void SetPageUpCanExecuteMonitor(DependencyObject obj, PageUpCanExecuteMonitor value)
    {
        obj.SetValue(PageUpCanExecuteMonitorProperty, value);
    }

    private static readonly DependencyProperty PageUpCanExecuteMonitorProperty =
        DependencyProperty.RegisterAttached("PageUpCanExecuteMonitor", typeof (PageUpCanExecuteMonitor), typeof (ScrollBarCommandsCanExecuteFixBehavior), new PropertyMetadata(null));

    #endregion

    #region PageDownCanExecuteMonitor Attached Property

    private static void SetPageDownCanExecuteMonitor(DependencyObject obj, PageDownCanExecuteMonitor value)
    {
        obj.SetValue(PageDownCanExecuteMonitorProperty, value);
    }

    private static readonly DependencyProperty PageDownCanExecuteMonitorProperty =
        DependencyProperty.RegisterAttached("PageDownCanExecuteMonitor", typeof (PageDownCanExecuteMonitor), typeof (ScrollBarCommandsCanExecuteFixBehavior), new PropertyMetadata(null));

    #endregion
}

, CommandBinding ScrollViewer CanExecute . e.CanExecute.

+2

Source: https://habr.com/ru/post/1787718/


All Articles