当前位置:   article > 正文

WPF ListView MVVM模式下数据增加自动滚动到底部_wpf listview自动滚动

wpf listview自动滚动

ListView 设置SelectedIndex属性不会滚动界面,只能通过ScrollIntoView方法设置,所以使用触发器检测
SelectedIndex ,使用扩展属性定义SelectedIndex的行为

引入dll

手动引入 System.Windows.Interactivity Microsoft.Expression.Interactions
xmal中添加声明

  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
  xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
  • 1
  • 2

定义扩展属性行为

    public static class ScrollToSelectedBehavior
    {
        public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.RegisterAttached(
            "SelectedValue",
            typeof(object),
            typeof(ScrollToSelectedBehavior),
            new PropertyMetadata(null, OnSelectedValueChange));

        public static void SetSelectedValue(DependencyObject source, object value)
        {
            source.SetValue(SelectedValueProperty, value);
        }

        public static object GetSelectedValue(DependencyObject source)
        {
            return (object)source.GetValue(SelectedValueProperty);
        }

        private static void OnSelectedValueChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var listbox = d as ListBox;
            listbox.ScrollIntoView(e.NewValue);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

定义数据触发器

  <i:Interaction.Triggers>
                <ei:DataTrigger Binding="{Binding Items.Count, ElementName=list}" Comparison="NotEqual" Value="0">
                    <ei:ChangePropertyAction TargetName="list" PropertyName="SelectedIndex"    Value="{Binding ElementName=list, Path=Items.Count}">
                    </ei:ChangePropertyAction>
                </ei:DataTrigger>
</i:Interaction.Triggers>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

测试

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="Window1" Height="300" Width="400">
    <StackPanel Orientation="Vertical">
        <ListView Height="200" ItemsSource="{Binding Itmes}" x:Name="list"  local:ScrollToSelectedBehavior.SelectedValue="{Binding ElementName=list, Path=SelectedValue}" >
            <i:Interaction.Triggers>
                <ei:DataTrigger Binding="{Binding Items.Count, ElementName=list}" Comparison="NotEqual" Value="0">
                    <ei:ChangePropertyAction TargetName="list" PropertyName="SelectedIndex"    Value="{Binding ElementName=list, Path=Items.Count}">
                    </ei:ChangePropertyAction>
                </ei:DataTrigger>
            </i:Interaction.Triggers>
        </ListView>
        <Button Width="75" Height="30" Click="Button_Click"> 新增</Button>
    </StackPanel>
</Window>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/537724
推荐阅读
相关标签
  

闽ICP备14008679号