A 有 ListBox 和 4 个项目。2 可见 2 已折叠:
点击:
- 这太糟糕了!
我需要这个:
我需要在重复按钮更改间隔中设置!?!?怎么做
A 有 ListBox 和 4 个项目。2 可见 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 的方法来完成我需要的操作。