相关疑难解决方法(0)

WPF CollectionViewSource多个视图?

我已经编写了带有搜索扩展名的自定义WPF控件,让我们为它命名MyControl.控制是一个ItemsControl 阶级的后代.

所以我像这样将数据源提供给它:

控件本身使用

protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
{
    if (newValue != null)
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
        view.Filter += this.FilterPredicate;
    }

    if (oldValue != null)
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(oldValue);
        view.Filter -= this.FilterPredicate;
    }

    base.OnItemsSourceChanged(oldValue, newValue);
}
Run Code Online (Sandbox Code Playgroud)

过滤源集合的视图(从而在内部ListBox中显示它).

现在假设我们在XAML中定义了10个具有相同DynamicSource的MyControl.问题是如果其中一个在源集合上应用Filter,它也会影响所有其他实例.

如何更改控件以避免此行为?

wpf controls filter collectionviewsource

17
推荐指数
2
解决办法
1万
查看次数

ICollectionView的SourceCollection为null

我有一个带有两个ICollectionViews 的ViewModel,它们被绑定为ItemsSource两个不同的ListBoxes.两者都包装相同ObservableCollection,但使用不同的过滤器.最初一切正常,两个ListBox都正确填充.

但是,当我更改ObservableCollection中的项并修改与过滤相关的属性时,ListBoxes不会更新.在调试器中,我发现两个ICollectionVIews的SourceCollection都为null,尽管我的ObservableCollection仍然存在.

这是我修改项目的方法,确保通过删除和添加相同的项目来更新ICollectionViews:

private void UnassignTag(TagViewModel tag)
{
    TrackChangedTagOnCollectionViews(tag, t => t.IsAssigned = false);
}

private void TrackChangedTagOnCollectionViews(TagViewModel tag, Action<TagViewModel> changeTagAction)
{
    _tags.Remove(tag);

    changeTagAction.Invoke(tag);

    _tags.Add(tag);
}
Run Code Online (Sandbox Code Playgroud)

该机制适用于我使用相同类的另一个上下文.

此外,我意识到如果我在ICollectionViews的CollectionChanged事件上注册监听器,问题就会消失.我确保从GUI线程创建和修改它们并怀疑垃圾收集是问题所在,但目前我被卡住了......想法?

更新:

在调试时我意识到在我调用ShowDialog()托管UserControl的WinForms表单之前,SourceCollections仍然存在.显示对话框后,它们就消失了.

我像这样创建ICollectionViews:

AvailableTags = new CollectionViewSource { Source = _tags }.View;
AssignedTags = new CollectionViewSource { Source = _tags }.View;
Run Code Online (Sandbox Code Playgroud)

这是我如何绑定其中一个(另一个非常相似):

<ListBox Grid.Column="0"  ItemsSource="{Binding AvailableTags}" Style="{StaticResource ListBoxStyle}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border Style="{StaticResource ListBoxItemBorderStyle}">
                        <DockPanel>
                            <Button DockPanel.Dock="Right" ToolTip="Assign" Style="{StaticResource IconButtonStyle}"
                                            Command="{Binding Path=DataContext.AssignSelectedTagCommand, …
Run Code Online (Sandbox Code Playgroud)

c# wpf

6
推荐指数
2
解决办法
1724
查看次数

使用实时整形的PresentationFramework中的"对象引用未设置为对象的实例"

我在LifeShaping过滤的PresentationFramework中得到一个空引用:

在此输入图像描述

堆栈跟踪没有给我很多线索:

   at System.Windows.Data.ListCollectionView.RestoreLiveShaping()
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, …
Run Code Online (Sandbox Code Playgroud)

c# wpf collectionviewsource

5
推荐指数
0
解决办法
1424
查看次数

从 ViewModel 绑定到反向 ObservableCollection

首先,我想说,在对 2 个不同的线程(如下所示)进行了几次研究之后,我决定发布这个问题,因为它完全不同。


因此,我想将ItemsControl视图中的an 绑定到一个属性以获取集合的反向版本。

我有这个观点(为了清晰起见进行了修剪):

<UserControl x:Class="NumberedMusicalScoresWriter.V.NotationGroupV" ...>
    ...
        <Grid>
            <ItemsControl ... 
                      ItemsSource="{Binding ReversedNotationVMs, Mode=OneWay}">
                ...
            </ItemsControl>
        </Grid>
    ...
</UserControl>
Run Code Online (Sandbox Code Playgroud)

而且,我有这个视图模型(为了清晰起见进行了修剪):

public class NotationGroupVM : ...
{
    ...

    public ObservableCollection<NotationVM> ReversedNotationVMs
    {
        get { return (ObservableCollection<NotationVM>)NotationVMs.Reverse(); //ERROR!! }
    }

    public ObservableCollection<NotationVM> NotationVMs
    {
        get { return _notationVMs; }
        set { _notationVMs = value; NotifyPropertyChanged("NotationVMs"); NotifyPropertyChanged("ReversedNotationVMs"); }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是有这个错误(请参阅上面的错误注释以发现有问题的行):

无法转换类型为“d__a0 1[NumberedMusicalScoresWriter.VM.NotationVM]' to type 'System.Collections.ObjectModel.ObservableCollection1[NumberedMusicalScoresWriter.VM.NotationVM]”的对象。

我还尝试.ToList<NotationVM>()在倒车之前申请,并在每次主字段更新时创建一个新集合。但他们没有成功。

我还需要保持反转与未反转的同步。不只是一次还原

我也在这里这里阅读了一个关于它的问题,但它们都只提供了 xaml 解决方案,或者我不理解它们。我需要一台虚拟机。

谢谢。

c# wpf xaml reverse mvvm

2
推荐指数
1
解决办法
1444
查看次数

标签 统计

wpf ×4

c# ×3

collectionviewsource ×2

controls ×1

filter ×1

mvvm ×1

reverse ×1

xaml ×1