Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DesignTime data not showing in Blend when bound against CollectionViewSource

I have a datatemplate for a viewmodel where an itemscontrol is bound against a CollectionViewSource (to enable sorting in xaml).

<DataTemplate x:Key="equipmentDataTemplate">
    <Viewbox>
        <Viewbox.Resources>
            <CollectionViewSource x:Key="viewSource" Source="{Binding Modules}">
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription PropertyName="ID" Direction="Ascending"/>
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>
        </Viewbox.Resources>
        <ItemsControl ItemsSource="{Binding Source={StaticResource viewSource}}" 
                      Height="{DynamicResource equipmentHeight}" 
                      ItemTemplate="{StaticResource moduleDataTemplate}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Viewbox>
</DataTemplate>

I have also setup the UserControl where all of this is defined to provide designtime data

d:DataContext="{x:Static vm:DesignTimeHelper.Equipment}">

This is basically a static property that gives me an EquipmentViewModel that has a list of ModuleViewModels (Equipment.Modules). Now as long as I bind to the CollectionViewSource the designtime data does not show up in blend 3. When I bind to the ViewModel collection directly

<ItemsControl ItemsSource="{Binding Modules}"

I can see the designtime data. Any idea what I could do?

like image 520
bitbonk Avatar asked Sep 19 '09 06:09

bitbonk


1 Answers

Been there done that (now at least) :)

This is the solution I've found. The trick is to override the source for the CollectionViewSource designtime. I use the d:DesignSource property to use another static resource designtime:

<Window.Resources>
    <CollectionViewSource x:Key="ViewSource"
            Source="{Binding ModelProperty}"
            d:DesignSource="{{x:Static MyProg:DesignTimeData.MyList}">
        <!-- Contents -->
    </CollectionViewSource>
</Window.Resources>
    
<!-- No change to the using class -->
<ListBox ItemsSource="{Binding Source={StaticResource ViewSource}}">
        
</ListBox>
like image 163
Almund Avatar answered Oct 20 '22 10:10

Almund