简体   繁体   中英

Xamarin - Having problems binding a ObservableCollection within another ObservableCollection in a CollectionView

I have a CollectionView that is bound to a ObserableCollection called UserPermissions. In that FrontUserPermissionsModel class I have another ObservableCollection called MyPermissions.

I am trying to do something like this:

                <CollectionView x:Name="CollectionViewEmployees" Margin="0,10,0,0" ItemsSource="{Binding UserPermissions}" SelectionMode="Single" Grid.Row="0"
                    SelectionChangedCommand ="{Binding SelectedUSerPermissionsDetailsCommand}" SelectedItem="{Binding SelectedUSerPermission}">
                   <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <StackLayout Padding="5,3,5,9">
 .
 .
 .
 .

                                     <StackLayout x:Name="SettingItemsLayout" Grid.Row="7" BackgroundColor="red" Grid.Column="0" HeightRequest="300" Grid.ColumnSpan="2" 
                                                         BindableLayout.ItemsSource="{Binding MyPermissions}"  HorizontalOptions="FillAndExpand">
                                                <BindableLayout.ItemTemplate>
                                                    <DataTemplate>
                                                        <Grid>
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition Width="52*" />
                                                                <ColumnDefinition Width="48*" />
                                                            </Grid.ColumnDefinitions>
                                                            <Label FontSize="18" Grid.Column="0" BackgroundColor="Green" FontAttributes="Bold" HorizontalTextAlignment="Start" Text="{Binding Title}" TextColor="white" />
                                                            <Label FontSize="18" Grid.Column="1" FontAttributes="Bold" HorizontalTextAlignment="Start" Text="{Binding AccessLevel}" TextColor="white" />
                                                        </Grid>
                                                    </DataTemplate>
                                                </BindableLayout.ItemTemplate>
                                            </StackLayout>
                            </StackLayout>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

The UserPermission binding is working. But my syntax on how to bind the MyPermissions is incorrect and I am not sure how to fix it. Thanks.

Do you mean you want to let your StackLayout binding an other ObserableCollection in the viewmodel?

You could use Binding Path :

Maybe something like:

<StackLayout x:Name="SettingItemsLayout" Grid.Row="7" BackgroundColor="red" Grid.Column="0" HeightRequest="300" Grid.ColumnSpan="2" 
                                                     BindableLayout.ItemsSource="{Binding Source={x:Reference SettingItemsLayout},
     Path=BindingContext.MyPermissions}"  HorizontalOptions="FillAndExpand">
      <BindableLayout.ItemTemplate>
           <DataTemplate>
                <Grid>
                  <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="52*" />
                     <ColumnDefinition Width="48*" />
                  </Grid.ColumnDefinitions>
                     <Label FontSize="18" Grid.Column="0" BackgroundColor="Green" FontAttributes="Bold" HorizontalTextAlignment="Start" Text="{Binding Title}" TextColor="white" />
                     <Label FontSize="18" Grid.Column="1" FontAttributes="Bold" HorizontalTextAlignment="Start" Text="{Binding AccessLevel}" TextColor="white" />
                </Grid>
           </DataTemplate>
       </BindableLayout.ItemTemplate>
</StackLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM