This is a migrated thread and some comments may be shown as answers.

Problem using CollectionViewSource view as ItemsSource of RadGridView

2 Answers 64 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alan
Top achievements
Rank 1
Alan asked on 19 Sep 2012, 09:31 PM
I am trying out RAD Silverlight controls and am running into trouble using a CollectionViewSource view as the ItemsSource for a GridView.
 The XAML code:
<telerik:RadGridView
    x:Name="agentGrid"
    Margin="416,8,192,0"
    CanUserFreezeColumns="False"
    RowIndicatorVisibility="Collapsed"
    ItemsSource="{Binding AgentsCv.View}"
    CanUserSelect="True"
    IsSynchronizedWithCurrentItem="True"
    SelectedItem="{Binding SelectedAgent, Mode=TwoWay}"
    ShowGroupPanel="False"
    AutoGenerateColumns="False">
 
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="First Name"
                                    DataMemberBinding="{Binding FirstName}"
                                    Width="120" />
        <telerik:GridViewDataColumn Header="Last Name"
                                    DataMemberBinding="{Binding LastName}"
                                    Width="200" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

Code in the ViewModel constructor:
AgentsInternal=new ObservableCollection<Agent>();
AgentsCv = new CollectionViewSource {Source = AgentsInternal};

ObservableCollection and CollectionViewSource code:

public CollectionViewSource AgentsCv { get; set; }
 
public ObservableCollection<Agent> AgentsInternal
{
    get { return _agentsInternal; }
    set
    {
        if (_agentsInternal == value) return;
        _agentsInternal = value;
        RaisePropertyChanged(() => AgentsInternal);
    }
}
private ObservableCollection<Agent> _agentsInternal;

The ObservableCollection is populated elsewhere via RIA Services.  LastName and FirstName are public properties of the Agent class.

When bound directly to the ObservableCollection, the GridView displays the data properly.  When bound to the CollectionViewSource view, no data appears in the GridView.

2 Answers, 1 is accepted

Sort by
0
André
Top achievements
Rank 1
answered on 20 Jun 2014, 09:30 AM
Try to set the CollectionViewSource in XAML:

<phone:PhoneApplicationPage.Resources>
    <CollectionViewSource x:Key="src" Source="{Binding Path=AgentsCv.View}"/>
</phone:PhoneApplicationPage.Resources>
 
<telerik:RadGridView
    x:Name="agentGrid"
    Margin="416,8,192,0"
    CanUserFreezeColumns="False"
    RowIndicatorVisibility="Collapsed"
    ItemsSource="{Binding Source={StaticResource src}}"
    CanUserSelect="True"
    IsSynchronizedWithCurrentItem="True"
    SelectedItem="{Binding SelectedAgent, Mode=TwoWay}"
    ShowGroupPanel="False"
    AutoGenerateColumns="False">
  
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="First Name"
                                    DataMemberBinding="{Binding FirstName}"
                                    Width="120" />
        <telerik:GridViewDataColumn Header="Last Name"
                                    DataMemberBinding="{Binding LastName}"
                                    Width="200" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

Code in the ViewModel constructor:
AgentsCv = Resources["src"] as CollectionViewSource;

greetings SunboX
0
Dimitrina
Telerik team
answered on 23 Jun 2014, 11:16 AM
Hello,

CollectionViewSource.View returns ICollectionView which is a valid Source collection for RadGridView. You should ensure there are items in the View as you assign it to the control and you should also raise the proper notification once you change the source.

For example:
private CollectionViewSource agentsCv;
 
public CollectionViewSource AgentsCv
{
    get { return this.agentsCv; }
    set
    {
        if (value != this.agentsCv)
        {
            this.agentsCv = value;
            this.OnPropertyChanged("AgentsCv");
        }
    }
}

Once the PropertyChanged notification is raised, RadGridView will load the data.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Alan
Top achievements
Rank 1
Answers by
André
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or