2011-01-25 26 views
3

私はリストボックスを使用するタッチスクリーンインターフェイスを作成しています。
ページの上下にリストボックスの上下にボタンがあります。Wpfは、上/下にスクロールするとリピートボタンを無効にします。

私は、ページアップボタンを全部スクロールして無効にする場所に移動しようとしています。
スクロールしてpagedownボタンを無効にすると、

は、ここでリストボックス

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

のための私のStyles.xamlのコードだと、ここで私は運をすべて昨日の周りに検索

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

リストボックスにインスタンスところです。
私はxamlですべてをやり遂げたいと考えています。

私はちょうどそのためPageUpCommandCanExecute方法を使用して...彼らは本当にのように見える

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

答えて

2

をボタンの画像を使用しますが上記の読みやすさのためにそれらを取り出した、
です。 falseを返します。ページが残っていない場合、ボタンは自動的に無効になります。

EDIT:

私はこの問題を解決するために使用することができ、簡単なattached behaviorを作成しました。ただ、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 
} 

基本的な考え方は、私たちはそれぞれのためにScrollViewerCommandBindingを追加することですこれらのバインディングのCanExecuteイベントを購読します。イベントハンドラでは、スクロールの現在位置を確認し、それに応じてe.CanExecuteプロパティを設定します。

+0

優れています。完璧に動作します。 LIneLeftとLineRightボタンを無効にしなければならず、ちょっとした変更を加えても期待通りに機能します。 ありがとうございました! –

+0

私はこのコードをComboBoxのポップアップに使用しています。私はLineUpとLineDownコマンドにこれを使用しようとしました。私がそれをそのまま使用すると、スクロールビューアの上部に到達すると、ポップアップが崩壊します。私が "e.Handled = true"をコメントアウトすると、それは隠されませんが、私が持っているリピートボタンが無効になっていないので、canExecuteは真のままです。だから、このコードは私のために働いていません。 –

関連する問題