wpf mvvm datagrid数据过滤

原文: wpf mvvm datagrid数据过滤

datagrid数据过滤,你可以通过设置RowStyle属性,通过将Visibility绑定到ViewModel层的属性来控制是否可见,比如:

  <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="Visibility">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource DataGridRowVisibilityConverter}">
                                <Binding ElementName="dataGrid" Path="DataContext.CurrentType"/>
                                <Binding Path="Type"/>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.RowStyle>

这里我利用DataGridRowVisibilityConverter这个转换器,当CurrentType=“all”时所有的都显示,当CurrentType不是all时,要求datagrid数据源里面某个数据的Type值和CurrentType一致,才显示。

但是这样做有一个问题,就是当行的可见性变更后,数据并不会重刷,datagrid的row索引还是老的,需要手动刷新,但是我们是mvvm模式,没法手动刷新datagrid的数据。我暂时还没找到解决这个问题的方法。

针对过滤,其实官方提供了一个解决方案,就是利用ICollectionView。该接口包含了一个Refresh方法,同时包含一个filter属性,该属性是用来过滤的,使用的时候,后台数据这么写:

public ICollectionView ViewSource { set; get; }

在ViewModel的构造方法里面这么写:

ViewSource = System.Windows.Data.CollectionViewSource.GetDefaultView(GlobalData.StatusList);
            ViewSource.Filter = new Predicate<object>(OnFilterMovie);
        }
        bool OnFilterMovie(object item)
        {
            if (CurrentType == "all") return true;
            else return (item as MovieModel).Type == CurrentType;
        }

然后CurrentType属性这么写,当变更时,调用ViewSource的Refresh方法刷新一次数据

 private string currentType = "all";
        public string CurrentType
        {
            get => currentType;
            set
            {
                if (currentType != value)
                {
                    currentType = value;
                    ViewSource.Refresh();
                }
            }
        }

扫描二维码关注公众号,回复: 4572153 查看本文章

而前端绑定还按照普通绑定的方法写就可以了:

 <DataGrid SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding ViewSource}" >

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/10147176.html
今日推荐