CollectionView 仅在第一次排序(当再次创建视图模型时它不会)
CollectionView only sorting first time (when create viewmodel again it doesnt)
我对 CollectionView
的理解有问题。我用一些 sorting/grouping 实现了它。但它只是在第一次创建视图时进行排序。
查看:
<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();
}
...
}
编辑:当我第一次查看视图时,集合已排序。如果我在那之后创建一个新的 Viewmodel,排序不会发生。
经过一些调试后,我发现 Sortdescriptions 丢失了。在构造函数中,我添加了 2 个 Sortdescription 和 1 个 groupdescription。一段时间后,我只剩下 groupdescription,排序也不见了,我不知道为什么 :(
http://imgur.com/U7362YP
-某处 SortDescriptions 是 lost/deleted
另一个问题,是否可以添加一个SortDescription
,如varType.id
?
天哪,我找到了解决方案。
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();
}));
似乎 View 注意到源已更新并删除了排序描述,但发生延迟。所有要做的就是添加 sortdescription also delayed
我对 CollectionView
的理解有问题。我用一些 sorting/grouping 实现了它。但它只是在第一次创建视图时进行排序。
查看:
<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();
}
...
}
编辑:当我第一次查看视图时,集合已排序。如果我在那之后创建一个新的 Viewmodel,排序不会发生。
经过一些调试后,我发现 Sortdescriptions 丢失了。在构造函数中,我添加了 2 个 Sortdescription 和 1 个 groupdescription。一段时间后,我只剩下 groupdescription,排序也不见了,我不知道为什么 :(
http://imgur.com/U7362YP -某处 SortDescriptions 是 lost/deleted
另一个问题,是否可以添加一个SortDescription
,如varType.id
?
天哪,我找到了解决方案。
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();
}));
似乎 View 注意到源已更新并删除了排序描述,但发生延迟。所有要做的就是添加 sortdescription also delayed