WPF - 'System.Windows.Data.CollectionViewSource' is not a valid value for property 'ItemsSource' using C#

csharp

(afree) #1

i have a WPF DataGrid which i should use a CollectionViewSource as the source of the items, the DataGrid is defined this way:

<DataGrid Name="lst_data" BorderThickness="0" Margin="0,5,0,0" Background="White" 
          ItemsSource="{StaticResource MyCVS}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="PCD" Width="*" Binding="{Binding PCD}"/>
        <DataGridTextColumn Header="A Group" Width="*" Binding="{Binding AGroup}"/>
        <DataGridTextColumn Header="Beg Effective" Width="*" Binding="{Binding BegEffective}"/>
        <DataGridTextColumn Header="End Effective" Width="*" Binding="{Binding EndEffective}" />
        <DataGridTextColumn Header="Line" Width="*" Binding="{Binding LineNum}"/>
    </DataGrid.Columns>
</DataGrid>

And the CollectionViewSource is defined in the Same Window Resources section like this:

<Window.Resources>
    <CollectionViewSource Source="{Binding ModelView1.DataSet.Local, Source={StaticResource Locator}}" x:Key="MyCVS" />
</Window.Resources>

The code above fails at the ItemsSource binding and it give this error:

‘System.Windows.Data.CollectionViewSource’ is not a valid value for property ‘ItemsSource’.

Any help please! this is urgent.

thanks


(this.is.sparta) #2

Here is what happens, The problem is that you are binding the ItemsSource property into a StaticResource directly. that can be easily solved by using a Binding instead of applying direct StaticResource Value.

So change your DataGrid XAML to something like this:

<DataGrid Name="lst_data" BorderThickness="0" Margin="0,5,0,0" Background="White" 
          ItemsSource="{Binding Source={StaticResource MyCVS}}" 
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="PCD" Width="*" Binding="{Binding PCD}"/>
        <DataGridTextColumn Header="A Group" Width="*" Binding="{Binding AGroup}"/>
        <DataGridTextColumn Header="Beg Effective" Width="*" Binding="{Binding BegEffective}"/>
        <DataGridTextColumn Header="End Effective" Width="*" Binding="{Binding EndEffective}" />
        <DataGridTextColumn Header="Line" Width="*" Binding="{Binding LineNum}"/>
    </DataGrid.Columns>
</DataGrid>

Notice this line:

ItemsSource="{Binding Source={StaticResource MyCVS}}" 

Hope that helps, solving your problem.


(Sine) #3

try to use this:

ItemsSource="{Binding Source={StaticResource MyCVS}}" 

instead of this:

ItemsSource="{StaticResource MyCVS}"

(afree) #4

That was fast, thanks @sparta . it solved my problem.