Problem using CollectionViewSource view as ItemsSource of RadGridView

3 posts, 0 answers
  1. Alan
    Alan avatar
    29 posts
    Member since:
    Feb 2008

    Posted 19 Sep 2012 Link to this post

    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. André
    André avatar
    8 posts
    Member since:
    May 2014

    Posted 20 Jun 2014 in reply to Alan Link to this post

    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
  3. DevCraft banner
  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 23 Jun 2014 Link to this post

    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.
     
Back to Top