リストボックスと4つのアイテムがあります。2つが表示されます2つが折りたたまれています:
クリック:
-これは悪い!
私はこれを必要とする:
reapeatButtonの変更間隔に設定する必要があります!?!?どうやってするの
リストボックスと4つのアイテムがあります。2つが表示されます2つが折りたたまれています:
クリック:
-これは悪い!
私はこれを必要とする:
reapeatButtonの変更間隔に設定する必要があります!?!?どうやってするの
必要なのは、繰り返しボタンをクリックするたびに、リスト ボックスが 2 行ずつスクロールすることです。ListBox
これは、まさにそれを行うために追加できる動作です。
最初にこの名前空間を追加します。
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
およびプロジェクトへの対応する参照。
次に、XAML は次のようになります。
<ListBox ScrollViewer.VerticalScrollBarVisibility="Visible" Height="40">
<i:Interaction.Behaviors>
<local:ScrollBehavior LineMultiplier="2"/>
</i:Interaction.Behaviors>
<ListBoxItem Content="Item1"/>
<ListBoxItem Content="Item2"/>
<ListBoxItem Content="Item3"/>
<ListBoxItem Content="Item4"/>
</ListBox>
そしてここに動作があります:
class ScrollBehavior : Behavior<FrameworkElement>
{
public int LineMultiplier
{
get { return (int)GetValue(LineMultiplierProperty); }
set { SetValue(LineMultiplierProperty, value); }
}
public static readonly DependencyProperty LineMultiplierProperty =
DependencyProperty.Register("LineMultiplier", typeof(int), typeof(ScrollBehavior), new UIPropertyMetadata(1));
protected override void OnAttached()
{
AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded);
}
private ScrollViewer scrollViewer;
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
scrollViewer = GetScrollViewer(AssociatedObject);
scrollViewer.CommandBindings.Add(new CommandBinding(ScrollBar.LineUpCommand, LineCommandExecuted));
scrollViewer.CommandBindings.Add(new CommandBinding(ScrollBar.LineDownCommand, LineCommandExecuted));
}
private void LineCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == ScrollBar.LineUpCommand)
{
for (int i = 0; i < LineMultiplier; i++)
scrollViewer.LineUp();
}
if (e.Command == ScrollBar.LineDownCommand)
{
for (int i = 0; i < LineMultiplier; i++)
scrollViewer.LineDown();
}
}
private ScrollViewer GetScrollViewer(DependencyObject o)
{
if (o is ScrollViewer)
return o as ScrollViewer;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
{
var result = GetScrollViewer(VisualTreeHelper.GetChild(o, i));
if (result != null)
return result;
}
return null;
}
}
アイテムごとにスクロールする必要があるアプリケーションがあったため、scrollviewer.isvirtualizing を true に設定するだけで十分でした。
ただし、ドックパネルで同様の動作を実装する必要があったため、必要なことを達成するために Rick Sladkey の方法を使用しました。