Mock data not showing when bound to ICollectionView

我的梦境 提交于 2019-12-02 09:36:09

问题


If I bound my ListBox to ViewModels ObservableCollection or XAML resourced CollectionViewSource, the mock data shows while in design.

Sometimes CollectionViewSource stops showing this data because of some XAML changes, but after rebuilding the code it fills controls back with fake data again.

Grouping, sorting and filtering in my case are controlled in ViewModel (and retried from database) so I decided to move over to ICollectionView property based in ViewModel. Unfortunately Views are no longer getting mock data at all.

Here is simple example of my approaches:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test"

        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance local:MainWindowViewModel }"

        Title="MainWindow" Height="100" Width="525"
    >

    <Window.Resources>

        <CollectionViewSource x:Key="ItemsCollectionViewSource" Source="{Binding ItemsObservableCollection}"/>

    </Window.Resources>

    <UniformGrid Columns="6">

        <ListBox ItemsSource="{Binding ItemsObservableCollection}" Background="WhiteSmoke" />

        <ListBox ItemsSource="{Binding Source={StaticResource ItemsCollectionViewSource}}" Background="LightYellow" />

        <ListBox ItemsSource="{Binding ItemsICollectionView}" Background="WhiteSmoke" />

        <ListBox ItemsSource="{Binding ItemsCollectionView}" Background="LightYellow" />

        <ListBox ItemsSource="{Binding ItemsListCollectionView}" Background="WhiteSmoke" />

        <ListBox ItemsSource="{Binding ItemsBackCollectionViewSource}" Background="LightYellow" />

    </UniformGrid>

</Window>

code behind:

namespace Test
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            DataContext = new MainWindowViewModel();

            InitializeComponent();
        }
    }
}

and ViewModel:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;

namespace Test
{
    public class MainWindowViewModel
    {
        public ICollectionView ItemsICollectionView { get; set; }

        public CollectionView ItemsCollectionView { get; set; }

        public ListCollectionView ItemsListCollectionView { get; set; }

        public ObservableCollection<string> ItemsObservableCollection { get; set; }

        public CollectionViewSource ItemsBackCollectionViewSource { get; set; }

        public MainWindowViewModel()
        {
            ItemsObservableCollection = new ObservableCollection<string> {"a", "b", "c"};

            ItemsICollectionView = CollectionViewSource.GetDefaultView(ItemsObservableCollection);

            ItemsCollectionView = CollectionViewSource.GetDefaultView(ItemsObservableCollection) as CollectionView;

            ItemsListCollectionView = CollectionViewSource.GetDefaultView(ItemsObservableCollection) as ListCollectionView;

            ItemsBackCollectionViewSource = new CollectionViewSource {Source = ItemsObservableCollection};
        }
    }
}

None of methods I have tried in order to move CollectionViewSource to ViewModel allows me to see mock data:

I did some debug comparison on those controls, but they are set pretty same in a run time. I'm not aware of ability to debug at design time.

Is there something I'm missing, or it has to be that way? Thanks

来源:https://stackoverflow.com/questions/38093415/mock-data-not-showing-when-bound-to-icollectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!