Mock data not showing when bound to ICollectionView
Asked Answered
B

0

1

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:

enter image description here

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

Bushtit answered 29/6, 2016 at 7:48 Comment(4)
Possible duplicate: stackoverflow.com/questions/1447962.Dd
related question is for blend, while I have issues with xaml editor (or maybe it is called blend as well?) also none of the answers there helps in solving this issue.Bushtit
I added more hours into this, still failing. Even creating CollectionViewSource as a property in code behind does not provide mock outcome. I was thinking that maybe it has something to do with CollectionViewType, but its value is null in both cases.Bushtit
To debug in design time (see this question: #27475747 ), you can open a visual studio with the same project and attach the debugger to the first visual studio. Set breakpoints in the second VS, refresh the design in the first... and voilá!Imprinting

© 2022 - 2024 — McMap. All rights reserved.