Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

BindCommand With Parameter ListView ReactiveUI Xamarin Forms

abbas25abbas25 Member ✭✭
edited October 14 in Xamarin.Forms

I am using Reactiveui with xamarin forms. I have Product Categories which is an observable collection getting bound to listview on xaml page. On click of a particular product category I need to get sub categories. The Following is the code of my xaml file, code behind file and the viewmodel file. I cannot get the Id of the product category clicked.

Xaml File

<ListView ItemsSource="{Binding ProductCategories}" HasUnevenRows="True" x:Name="lvProductCategories" SelectedItem="{Binding SelectedProductCategoryId}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ImageCell 
                            Text="{Binding CategoryName}">
                        </ImageCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
</ListView>

Code Behind File

public ProductCategoryPage()
{
            InitializeComponent();
            BindingContext = ViewModel = new ProductCategoryPageViewModel();

            this.BindCommand(ViewModel, vm => vm.CMDGetSubProductCategories, page => page.lvProductCategories, vm => vm.SelectedProductCategoryId ,nameof(lvProductCategories.ItemTapped));
}

ViewModel File

public class ProductCategoryPageViewModel : ReactiveObject
{
            private IProductCategoryService ProductCategoryService;
            public ProductCategoryPageViewModel(IProductCategoryService productCategoryService = null)
            {
                ProductCategoryService = productCategoryService ?? (IProductCategoryService)Splat.Locator.Current.GetService(typeof(IProductCategoryService));
                GetProductCategories();

                CMDGetSubProductCategories = ReactiveCommand.Create<int>(x =>
                {
                    GetSubProductCategories(x);
                });


            }


            ObservableCollection<ProductCategoryMaster> _productCategories;
            public ObservableCollection<ProductCategoryMaster> ProductCategories
            {
                get => _productCategories;
                set { this.RaiseAndSetIfChanged(ref _productCategories, value); }
            }

            int _selectedProductCategoryId;
            public int SelectedProductCategoryId
            {
                get => _selectedProductCategoryId;
                set => this.RaiseAndSetIfChanged(ref _selectedProductCategoryId, value);
            }

            public async void GetProductCategories()
            {
                var productCategories = await ProductCategoryService.GetAllProductCategories();
                ProductCategories = new ObservableCollection<ProductCategoryMaster>(productCategories);
            }

            public async void GetSubProductCategories(int pcid)
            {
                var productCategories = await ProductCategoryService.GetSubProductCategories(pcid);
                ProductCategories = new ObservableCollection<ProductCategoryMaster>(productCategories);
            }

            public ReactiveCommand<int,Unit> CMDGetSubProductCategories { get; }
}

I am new to xamarin forms and reactiveui. Thank you for your help.

Best Answers

  • abbas25abbas25 Member ✭✭
    Accepted Answer

    I would like to thank Jarvan for pointing me in correct direction.

    For anyone who has this problem I solved it using the following.

    Xaml.cs file

    private void listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
            var model = e.SelectedItem as ProducCategoryMaster; 
         var vm = BindingContext as ProductCategoryPageViewModel;
             vm.SelectedProductCategoryId = model.PKProductCategoryId;
    }
    
    

    Rest of the code remains same as I have posted in my question.

Answers

  • abbas25abbas25 Member ✭✭

    Hi Jarvan,

    Thank you for your reply. I need that Id to be sent to viewmodel so that I can use it there.

  • JarvanJarvan Member, Xamarin Team Xamurai

    @abbas25 said:
    Hi Jarvan,

    Thank you for your reply. I need that Id to be sent to viewmodel so that I can use it there.

    Do you want to call a method of the ViewModel class?

    public partial class ProductCategoryPage : ContentPage
    {
        ProductCategoryPageViewModel viewModel;
        public ProductCategoryPage()
        {
            InitializeComponent();
    
            viewModel = new ProductCategoryPageViewModel();
            BindingContext = viewModel;
        }
    
        private void listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var model = e.SelectedItem as CustomModel; //in your case, the model class should be ProductCategoryMaster
    
            int id = model.PKProductCategoryId;
    
            viewModel.Method(id);
        }
    }
    
  • abbas25abbas25 Member ✭✭

    Hi Jarvan,

    The thing is I am using reactiveui and I need to pass the id obtained as a parameter for reactiveui bindcommand.

    this.BindCommand(ViewModel, vm => vm.CMDGetSubProductCategories, page => page.lvProductCategories, vm => vm.PKProductCategoryId ,nameof(lvProductCategories.ItemTapped));

    in the above line

  • abbas25abbas25 Member ✭✭
    Accepted Answer

    I would like to thank Jarvan for pointing me in correct direction.

    For anyone who has this problem I solved it using the following.

    Xaml.cs file

    private void listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
            var model = e.SelectedItem as ProducCategoryMaster; 
         var vm = BindingContext as ProductCategoryPageViewModel;
             vm.SelectedProductCategoryId = model.PKProductCategoryId;
    }
    
    

    Rest of the code remains same as I have posted in my question.

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited October 14

    @abbas25 said:
    I would like to thank Jarvan for pointing me in correct direction.

    For anyone who has this problem I solved it using the following.

    Xaml.cs file

    private void listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
            var model = e.SelectedItem as ProducCategoryMaster; 
       var vm = BindingContext as ProductCategoryPageViewModel;
             vm.SelectedProductCategoryId = model.PKProductCategoryId;
    }
    
    

    Rest of the code remains same as I have posted in my question.

    Congrats! If the solution helps you to solve the issue, please click the 'OK' option to accept the helpful reply as the answer. It'll help others who face the similar problem.

Sign In or Register to comment.