CollectionView 仅在第一次排序(再次创建视图模型时不会)

本文关键字:模型 视图 创建 第一次 排序 CollectionView | 更新日期: 2023-09-27 18:36:48

我有问题无法理解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);
  • 每次我加载其他值等以重新加载选项卡时都会调用它。

选项卡项的实际视图模型:

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

编辑:当我第一次查看视图时,集合已排序。如果我在那之后创建一个新的视图模型,则不会进行排序。

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

https://i.stack.imgur.com/4HYp0.jpg-排序描述丢失/删除的地方

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

CollectionView 仅在第一次排序(再次创建视图模型时不会)

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

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

似乎注意到视图已更新源代码并删除了排序说明,但这延迟了。所有要做的就是添加排序描述也延迟