Forum Xamarin.Forms

ListView ItemsSource Binding in XAML Problem

when i change SelectedIndexChanged from picker i get data from Api to fill ListView but not Wotking
i try with & without fody.
SelectedIndexChanged is a behavoir picker

I use :
xamarin froms : 4.4.0.991537 v
PropertyChanged.Fody : 2.5.13 v

Listview Code :

                  <ListView  x:Name="RolesList" Margin="0,10,0,0"   ItemsSource="{Binding RolesUsers}">
                          <ListView.ItemTemplate>
                               <DataTemplate>
                                  <ViewCell>
                                        <StackLayout Orientation="Horizontal">
                                          <CheckBox HorizontalOptions="Start" IsChecked="{Binding IsSelected}" />
                                         <Label  HorizontalOptions="Start" Text="{Binding Name}" TextColor="Red" FontSize="13" />
                                         </StackLayout>
                                </ViewCell>
                              </DataTemplate>
                            </ListView.ItemTemplate>
                            <ListView.Footer>
                                 <StackLayout Padding="5,5,5,5">
                                      <Button
                                       BackgroundColor="Green"
                                          BorderRadius="5"
                                    Command="{Binding AddCommand}"
                                         CommandParameter="{Binding RolesUsers}"
                                       Text="Sauvegarder"
                                         TextColor="White"
                                        VerticalOptions="Center" />
                                </StackLayout>
                           </ListView.Footer>
                       </ListView>

ViewModel:

 public class ManagerRolesViewModel : BaseViewModel
    {

        public List<UserInfo> UserInfos { get; set; }

        public UserInfo SelectedUser { get; set; }
        public bool ListRolesVisibility { get; set; }
        public ObservableCollection<RoleForCheckBox> rolesUsers;


        public ObservableCollection<RoleForCheckBox> RolesUsers
        {
            get => rolesUsers;
            set => SetProperty(ref rolesUsers, value);
        }

        public ManagerRolesViewModel()
        {
           // this.TestCommand.Execute(null);

        }
        public ManagerRolesViewModel(string userId)
        {
            this.GetRolesByUserCommand.Execute(userId);
        }
        public override async void OnAppearing()
        {
            base.OnAppearing();

            this.GetUsersAndRolesCommand.Execute(null);
        }


        public ICommand GetRolesByUserCommand
        {
            get
            {
                return new Command<string>(async (userId) =>
                {
                    var result = await Api.GetListRolesByUsers(userId);
                    RolesUsers = result.data;
                    ListRolesVisibility = true;
                });
            }
        }
    }
}

PickerViewBehavior :

public class PickerViewBehavior : Behavior<Picker>
    {

        protected override void OnAttachedTo(Picker bindable)
        {
            bindable.SelectedIndexChanged += Bindable_SelectedIndexChanged;
        }

        protected override void OnDetachingFrom(Picker bindable)
        {
            bindable.SelectedIndexChanged -= Bindable_SelectedIndexChanged;
        }

        void Bindable_SelectedIndexChanged(object sender, EventArgs e)
        {
            UserInfo selectedUser = ((Picker)sender).SelectedItem as UserInfo;
            if (selectedUser == null)
            {
                ((Picker)sender).SelectedItem = null;
                return;
            }
            var vm = new ManagerRolesViewModel();
            vm.GetRolesByUserCommand.Execute(selectedUser.UserId);

        }
    }

Best Answers

Answers

  • MehdiMllMehdiMll Member ✭✭

    @JoeManke i make that : var vm = new ManagerRolesViewModel(selectedUser.UserId);
    also not work

Sign In or Register to comment.