Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
35
Getting selected row in view model
posted

Hello, I am a teacher and I am creating my first Xamarin app. I am using Prism for my MVVM framework and one of your grids. I have a screen listing all the sections I teach (bound to an observable collection in my view model). When the I select a section and click on a button I want to navigate to a page with a list of students in that section.

The trouble is that I don’t know what row was selected when I am in my view model. I don’t want to go into the code behind as shown in the documentation. I thought there would be a property in the grid (selected items?) that I could bind to my view model so that when I click the button I would know what section had been selected.

What would be the best way to achieve this with the Infragistics grid?

Parents
  • 33370
    Offline posted

    Hello Jim,

    I have been investigating into the behavior you are trying to achieve in this case, and currently the selected item of the XamDataGrid in Infragistics for Xamarin.Forms is not bindable, as the SelectedItems and SelectedKeys properties are not bindable properties.

    While these properties are not bindable at the moment, this is not to say that the selected items cannot be kept track of in an MVVM way. I would recommend continuing with what is listed in our documentation, but handle the SelectedItemsChanged event in a Behavior<XamDataGrid> that you can then add to the XamDataGrid.Behaviors collection. This will allow you to keep track of the selected items in an MVVM way. There is a good example of usage of Xamarin.Forms Behaviors here.

    Please let me know if you have any other questions or concerns on this matter.

  • 35
    Offline posted in reply to Andrew Goldenbaum

    Thanks for your help. My plan now is to use the attached behavior to publish an event with the value of the selected session. The view model will receive the event and store the latest selected session. My xaml do not what to see me attached behavior. 

    <ContentPage
        x:Class="CMMobile.Views.SectionPage"
        xmlns="
    ">xamarin.com/.../forms"
        xmlns:x="">schemas.microsoft.com/.../xaml"
        xmlns:igDataGrid="clr-namespace:Infragistics.XamarinForms.Controls.Grids;assembly=Infragistics.XF.DataGrid"
        xmlns:local="clr-namespace:CMMobile.Views;assembly=CMMobile"
        xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
        prism:ViewModelLocator.AutowireViewModel="True"
        Title="Section">
     
        <ContentPage.ToolbarItems>
            <ToolbarItem Text="Doit"></ToolbarItem>
        </ContentPage.ToolbarItems>
      
        <Grid>
            <igDataGrid:XamDataGrid ItemsSource="{Binding Path=SectionCollection}" SelectionMode="SingleRow" >
              
                <igDataGrid.Behaviors>
                    <local:SectionSelected/>
                </igDataGrid.Behaviors>
              
                <igDataGrid:XamDataGrid.Columns>
                    <igDataGrid:TextColumn
                        HeaderText="Name"
                        PropertyPath="Name"
                        SelectedBackground="#FFCD2929" />
                    <igDataGrid:TextColumn HeaderText="Period" PropertyPath="Period" />
                </igDataGrid:XamDataGrid.Columns>
            </igDataGrid:XamDataGrid>
        </Grid>
     
    </ContentPage>
    When I put in the <igDataGrid.Behaviors> tag, it was not picked up by intelisense so I am not sure I put things in the right place.
    It tells me: Error  Position 21:18. Type local:SectionSelected not found in xmlns clr-namespace:CMMobile.Views;assembly=CMMobile CMMobile D:\Repos\CMMobile\CMMobile\CMMobile\Views\SectionPage.xaml
    It is in that namespace as shown:
    namespace CMMobile
    {
       public class SectionSelected : Behavior<XamDataGrid>
        {
            IEventAggregator eventAg;
            public SectionSelected(IEventAggregator ea)
            {
                eventAg = ea;
            }
            protected override void OnAttachedTo(XamDataGrid grid)
            {
                grid.SelectedItemsChanged += Grid_SelectedItemsChanged;
                base.OnAttachedTo(grid);
            }
            private void Grid_SelectedItemsChanged(object sender, GridSelectedItemsChangedEventArgs args)
            {
                XamDataGrid grid = (sender as XamDataGrid);
                if (grid.SelectedItems[0] != null)
                {
                    Section selectedItem = grid.SelectedItems[0] as Section;
                    eventAg.GetEvent<SectionChangedEvent>().Publish(selectedItem);
                }
            }
            protected override void OnDetachingFrom(XamDataGrid grid)
            {
                grid.SelectedItemsChanged -= Grid_SelectedItemsChanged;
                base.OnDetachingFrom(grid);
            }
        }
    }
    Does my XAML look right?
    Thanks Jim
     
Reply Children