2
votes

I've got the following code that works well :

<Viewbox.Resources>
    <CollectionViewSource x:Key="viewSource"
                          Source="{Binding Path=SelectionList}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Description" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

</Viewbox.Resources>
<ComboBox ItemsSource="{Binding Source={StaticResource ResourceKey=viewSource}}"/>

I would like to put my CollectionViewSource directly in my ComboBox without using any resource like that :

<ComboBox SelectedItem="{Binding Path=Value, Mode=TwoWay}">
    <ComboBox.ItemsSource>
        <Binding>
            <Binding.Source>
                <CollectionViewSource Source="{Binding Path=SelectionList}">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="Description" />
                    </CollectionViewSource.SortDescriptions>
                </CollectionViewSource>
            </Binding.Source>
        </Binding>
    </ComboBox.ItemsSource>
</ComboBox>

But this way my ComboBox is always empty, and I get the following binding error :

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=SelectionList; DataItem=null; target element is 'CollectionViewSource' (HashCode=1374711); target property is 'Source' (type 'Object')

Does anyone know how could I do it ?

2
It's just a guess, but you may have to explicitly set the Source of the CollectionViewSource.Source's binding: <CollectionViewSource Source="{Binding Source=... Path=SelectionList}". Do you see any binding error messages? - Clemens
Thanks to your remark I checked my binding errors. In your suggestion what could I put in Source=... - Nicolas
Set it to the object that owns the SelectionList property. - Clemens
SelectionList is defined in the ViewModel/DataContext. I tried <CollectionViewSource Source="{Binding Source=., Path=Items}"> but it doesn't work. - Nicolas
@Nicolas By explicit, I think he means an non-binding source, such as {RelativeSource Self}, then bind to the DataContext.SelectionList property - Rachel

2 Answers

2
votes

Nicolas, although this is not an answer to your question because it still uses resources, you could put the CollectionViewSource inside the ComboBox by defining it in its local resource dictionary:

<ComboBox>
    <ComboBox.Resources>
        <CollectionViewSource x:Key="viewSource" Source="{Binding Path=SelectionList}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Description" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </ComboBox.Resources>
    <ComboBox.ItemsSource>
        <Binding Source="{StaticResource viewSource}"/>
    </ComboBox.ItemsSource>
</ComboBox>
1
votes

You're getting the error because the CollectionViewSource doesn't have a parent from which to inherit a DataContext from to use in the Binding.

You don't need to use a CollectionViewSource for your ComboBox though. You can bind it's item source to any collection by binding the ItemsSource property

<ComboBox ItemsSource="{Binding SelectionList}"
          SelectedItem="{Binding Path=Value, Mode=TwoWay}" />

The only thing missing would be the sorting, however you can sort the data in your ViewModel before returning it to the View.