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

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"

定义扩展属性行为

    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);
        }
    }

定义数据触发器

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

测试

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

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值