【问题标题】:CollectionView only sorting first time (when create viewmodel again it doesnt)CollectionView 仅在第一次排序(再次创建视图模型时不会)
【发布时间】:2015-01-09 10:01:50
【问题描述】:

我无法理解CollectionView。我通过一些排序/分组来实现它。但它只是在第一次创建视图时进行排序。

查看:

<DataGrid ItemsSource="{Binding varCollectionview}" Grid.Row="2" AutoGenerateColumns="False" SelectedItem="{Binding selVariable}" Grid.ColumnSpan="2">
        <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock Text="{Binding Name.value}" Foreground="White" Background="Gray" HorizontalAlignment="Stretch" TextAlignment="Left" Padding="10,0" />
                                        <ItemsPresenter></ItemsPresenter>
                                    </StackPanel>

                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>

                    </Style>
                </GroupStyle.ContainerStyle></GroupStyle>
        </DataGrid.GroupStyle>
        <i:Interaction.Behaviors>
            <behavior:DataGridScrollIntoViewBehavior />
        </i:Interaction.Behaviors>
        <DataGrid.Columns>
        ...
        ...

MainViewModel 调用:

variablenVm = new VariablenViewModel(cfg);
  • 每次我加载其他值等时都会调用它以重新加载选项卡。

TabItem 的实际 ViewModel:

public class VariablenViewModel : ViewModelBase
{
    private string _filterString;
    public string filterString 
    {
        get { return _filterString ; }
        set
        {
            _filterString = value.ToLower();
            RaisePropertyChanged("filterString ");
            varCollectionview.Refresh();
        }
    }
    private SimuVariable _selVariable;
    public SimuVariable selVariable
    {
        get { return _selVariable; }
        set
        {
            _selVariable = value;
            RaisePropertyChanged("selVariable");
        }
    }
    private Konfiguration _cfg;
    public Konfiguration cfg
    {
        get { return _cfg; }
        set
        {
            _cfg = value;
            RaisePropertyChanged("cfg");
        }
    }
    private ICollectionView _varCollectionview;
    public ICollectionView varCollectionview
    {
        get { return _varCollectionview; }
        set
        {
            _varCollectionview = value;
            RaisePropertyChanged("varCollectionview");
        }
    }

    public VariablenViewModel(Konfiguration _mainCfg)
    {
        cfg = _mainCfg;
        hasSomeValueChanged = false;
        _filterString = "";

        _neueVariableCommand = new RelayCommand(() =>
        {
...
        });
        _loescheVariableCommand = new RelayCommand(
...
        );
        varCollectionview = (CollectionView)CollectionViewSource.GetDefaultView(cfg.variablen);
        if (varCollectionview.GroupDescriptions.Count == 0)
        {
            varCollectionview.GroupDescriptions.Add(new PropertyGroupDescription("varType"));
        }
        if (varCollectionview.SortDescriptions.Count == 0)
        {
            varCollectionview.SortDescriptions.Add(new SortDescription("varType", ListSortDirection.Descending));
            varCollectionview.SortDescriptions.Add(new SortDescription("name", ListSortDirection.Ascending));
        }
        varCollectionview.Filter = new Predicate<object>(filter);
        varCollectionview.Refresh();
    }

    ...
    }

edit:当我第一次查看视图时,集合已排序。如果我在那之后创建一个新的 Viewmodel,则不会发生排序。

经过一些调试后,我发现 Sortdescriptions 丢失了。在构造函数中,我添加了 2 个排序描述和 1 个组描述。一段时间后,我只剩下组描述了,排序消失了,我不知道为什么:(

http://imgur.com/U7362YP -SortDescriptions 丢失/删除的地方

另一个问题,是否可以像varType.id一样添加SortDescription

【问题讨论】:

  • 你一直加SortDescription是什么意思?
  • @Default: not clear 不会改变行为,只是测试过 ->if (varCollectionview.SortDescriptions.Count == 0) -> add
  • @Aron:嗯,这种行为似乎已经消失了。但是有一天,在调用 GetDefaultView 之后,我得到了最后一个视图模型实例的排序描述。但这并没有改变 sortdescription 只对第一次创建这个 ViewModel 有影响的事实。
  • 刷新没有帮助。我注意到我第一次“查看”视图时排序是正确的,即使视图模型更改了几次。创建视图后,当我更改视图模型时,它不会排序
  • 您能提供更多代码吗?完整的构造函数代码可能会有所帮助。甚至可能是整个班级——至少是与 CollectionView 相关的成员。

标签: c# wpf sorting collectionview collectionviewsource


【解决方案1】:

是的,我找到了解决方案。

varCollectionview = new CollectionViewSource();
        varCollectionview.Source = cfg.variablen;
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new Action(delegate()
            {
        varCollectionview.View.SortDescriptions.Add(new SortDescription("varType", ListSortDirection.Descending));
        varCollectionview.View.SortDescriptions.Add(new SortDescription("name", ListSortDirection.Ascending));
        varCollectionview.View.GroupDescriptions.Add(new PropertyGroupDescription("varType"));
        varCollectionview.View.Filter = new Predicate<object>(filter);
        varCollectionview.View.Refresh();
            }));

似乎注意到视图已更新源并删除了排序描述,但这发生了延迟。只需添加同样延迟的排序描述

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多