afree
(afree)
September 16, 2016, 12:44am
#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
2 Likes
sparta
(this.is.sparta)
September 16, 2016, 1:32am
#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.
2 Likes
Sine
(Sine)
September 16, 2016, 1:44am
#3
try to use this:
ItemsSource="{Binding Source={StaticResource MyCVS}}"
instead of this:
ItemsSource="{StaticResource MyCVS}"
afree
(afree)
September 16, 2016, 1:55am
#4
That was fast, thanks @sparta . it solved my problem.