c# - MVVM WPF 中的列表框滚动事件

标签 c# wpf mvvm windows-phone windows-phone-7.1

有没有办法以 MVVM 方式 Hook WPF(Windows Phone7)中的滚动事件?我想检测列表何时滚动到底部,然后做一些事情。我试过这样的东西,但显然它行不通:

<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedPlace, Mode=TwoWay}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <i:InvokeCommandAction Command="{Binding ListBoxClick}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="Scroll">
            <i:InvokeCommandAction Command="{Binding ListBoxScroll}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    (...)
</ListBox>

最佳答案

在这种情况下,我总是着眼于附加行为的方向,因为我们需要一个独立的解决方案,可以在侧边 UI 和 MVVM 风格中工作。附加行为 - 是一个附加属性,它有一个事件处理程序来更改此属性,所有逻辑都在此处理程序中实现。

在这种情况下,您需要传递一个指示行为开始的 bool 值,以及命令,当我们的条件执行时要执行的内容 - 滚动到 ListBox 的末尾。

我创建了一个示例行为,其中关键逻辑在这里:

private static void scrollViewerScrollChanged(object sender, ScrollChangedEventArgs e)
{
    var scrollViewer = sender as ScrollViewer;

    if (scrollViewer != null)
    {
        // Here we determine if the bottom reached
        if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
        {
            command = GetCommand(listBox);

            // Execute the command
            command.Execute(listBox);
        }
    }
}

使用示例:

XAML

<Window.DataContext>
    <local:TestViewModel />
</Window.DataContext>

<Window.Resources>
    <x:Array x:Key="TestArray" Type="{x:Type sys:String}">
        <sys:String>Test1</sys:String>
        <sys:String>Test2</sys:String>
        <sys:String>Test3</sys:String>
        <sys:String>Test4</sys:String>
        <sys:String>Test5</sys:String>
        <sys:String>Test6</sys:String>
        <sys:String>Test7</sys:String>
        <sys:String>Test8</sys:String>
        <sys:String>Test9</sys:String>
        <sys:String>Test10</sys:String>
    </x:Array>
</Window.Resources>

<Grid>
    <ListBox Name="TestListBox"
             AttachedBehaviors:ScrollingToBottomBehavior.IsEnabled="True"
             AttachedBehaviors:ScrollingToBottomBehavior.Command="{Binding TestButtonCommand}"
             ItemsSource="{StaticResource TestArray}"
             Height="50" />
</Grid>

TestViewModel

public class TestViewModel
{
    private ICommand _testButtonCommand = null;

    public ICommand TestButtonCommand
    {
        get
        {
            if (_testButtonCommand == null)
            {
                _testButtonCommand = new RelayCommand(param => this.TestButton(), null);
            }

            return _testButtonCommand;
        }
    }

    private void TestButton()
    {
        MessageBox.Show("Test command execute");
    }
}

ScrollingToBottomBehavior

public class ScrollingToBottomBehavior
{
    #region Private Section

    private static ListBox listBox = null;
    private static ICommand command = null;

    #endregion

    #region IsEnabledProperty

    public static readonly DependencyProperty IsEnabledProperty;

    public static void SetIsEnabled(DependencyObject DepObject, string value)
    {
        DepObject.SetValue(IsEnabledProperty, value);
    }

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

    #endregion

    #region CommandProperty

    public static readonly DependencyProperty CommandProperty;

    public static void SetCommand(DependencyObject DepObject, ICommand value)
    {
        DepObject.SetValue(CommandProperty, value);
    }

    public static ICommand GetCommand(DependencyObject DepObject)
    {
        return (ICommand)DepObject.GetValue(CommandProperty);
    }

    static ScrollingToBottomBehavior()
    {
        IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled",
                                                            typeof(bool),
                                                            typeof(ScrollingToBottomBehavior),
                                                            new UIPropertyMetadata(false, IsFrontTurn));

        CommandProperty = DependencyProperty.RegisterAttached("Command",
                                                            typeof(ICommand),
                                                            typeof(ScrollingToBottomBehavior));
    }

    #endregion

    private static void IsFrontTurn(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        listBox = sender as ListBox;

        if (listBox == null)
        {
            return;
        }

        if (e.NewValue is bool && ((bool)e.NewValue) == true)
        {
            listBox.Loaded += new RoutedEventHandler(listBoxLoaded);
        }
        else 
        {
            listBox.Loaded -= new RoutedEventHandler(listBoxLoaded);
        }
    }

    private static void listBoxLoaded(object sender, RoutedEventArgs e)
    {
        var scrollViewer = GetFirstChildOfType<ScrollViewer>(listBox);

        if (scrollViewer != null)
        {
            scrollViewer.ScrollChanged += new ScrollChangedEventHandler(scrollViewerScrollChanged);
        }
    }

    #region GetFirstChildOfType

    private static T GetFirstChildOfType<T>(DependencyObject dependencyObject) where T : DependencyObject
    {
        if (dependencyObject == null)
        {
            return null;
        }

        for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
        {
            var child = VisualTreeHelper.GetChild(dependencyObject, i);

            var result = (child as T) ?? GetFirstChildOfType<T>(child);

            if (result != null)
            {
                return result;
            }
        }

        return null;
    }

    #endregion

    private static void scrollViewerScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        var scrollViewer = sender as ScrollViewer;

        if (scrollViewer != null)
        {
            if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
            {
                command = GetCommand(listBox);
                command.Execute(listBox);
            }
        }
    }
}

Complete sample project is available here.

关于c# - MVVM WPF 中的列表框滚动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21661821/

相关文章:

wpf - 选中/取消选中复选框时,带有复选框的列表框不会触发所选项目

wpf - 从代码隐藏设置用户控件的 DataContext

javascript - KendoUI MVVM 绑定(bind)根据显示值手动更新源

c# - 具有 10 英尺 gui 的多个文本元素的 WPF 按钮

c# - "An attempt was made to load a program with an incorrect format"

c# - 带有 TwoWay DataBinding (double) 的 TextBox 不允许使用小数点分隔符

c# - 在 WP7 中将 List<T> 转换为 ObservableCollection<T>

wpf - PRISM WPF - 导航每次都会创建新 View

c# - 使用 Renci.SshNet SftpClient 更改具有绝对路径的目录导致 SftpPathNotFoundException

wpf - 在wpf中使用aforge videosouceplayer