locked
Send 2 Labels Text value with Command inside BindableLayout RRS feed

  • Question

  • User386578 posted

    I'm working with MVVM and I have a bindablelayout like this:

        <StackLayout Margin="20" BindableLayout.ItemsSource="{Binding MyApartments}" >
                            <BindableLayout.ItemTemplate>
                                <DataTemplate>
    
                                    <StackLayout Margin="0,10,0,0">
                                        <Frame Padding="15" HasShadow="True" BackgroundColor="WhiteSmoke" HorizontalOptions="Fill" VerticalOptions="CenterAndExpand" HeightRequest="150" CornerRadius="15">
                                            <StackLayout x:Name="AptItems">
                                                <Label x:Name="buildinglabel" Text="{Binding building}" Margin="0,15,0,0" FontFamily="{StaticResource mon_extrabold}" TextColor="DarkBlue" FontSize="Title"/>
                                                <Label x:Name="numberlabel" Text="{Binding number}" FontFamily="{StaticResource mon_bold}" TextColor="DarkBlue" FontSize="Subtitle"/>
                                                <Button HorizontalOptions="End" FontFamily="{StaticResource mon_regular}" BackgroundColor="{StaticResource bounty}" TextColor="White" Command="{Binding Path=BindingContext.SelectApartment, Source={x:Reference ApartmentsPage}}" CommandParameter="{x:Reference AptItems}"/>
                                            </StackLayout>
                                        </Frame>
                                    </StackLayout>
    
                                </DataTemplate>
                            </BindableLayout.ItemTemplate>
                        </StackLayout>
    

    I need to save the value in buildinglabel and numberlabel to my preferences but I don't know how to pass both labels to a command but I'm really lost here and time is running out :(

    Sunday, September 15, 2019 7:32 PM

Answers

  • User369979 posted

    You never need to think about the UI part since you are consuming MVVM. You could pass the current binding context to your command and get the value through yourModel.building and yourModel.number. Adjust your button like:

    <Button HorizontalOptions="End" FontFamily="{StaticResource mon_regular}" BackgroundColor="{StaticResource bounty}" TextColor="White" Command="{Binding Path=BindingContext.SelectApartment, Source={x:Reference ApartmentsPage}}" CommandParameter="{Binding .}"/>
    

    Then you will retrieve the single binding context in your Command.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, September 16, 2019 6:22 AM

All replies

  • User369979 posted

    You never need to think about the UI part since you are consuming MVVM. You could pass the current binding context to your command and get the value through yourModel.building and yourModel.number. Adjust your button like:

    <Button HorizontalOptions="End" FontFamily="{StaticResource mon_regular}" BackgroundColor="{StaticResource bounty}" TextColor="White" Command="{Binding Path=BindingContext.SelectApartment, Source={x:Reference ApartmentsPage}}" CommandParameter="{Binding .}"/>
    

    Then you will retrieve the single binding context in your Command.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, September 16, 2019 6:22 AM
  • User386578 posted

    @LandLu said: You never need to think about the UI part since you are consuming MVVM. You could pass the current binding context to your command and get the value through yourModel.building and yourModel.number. Adjust your button like:

    <Button HorizontalOptions="End" FontFamily="{StaticResource mon_regular}" BackgroundColor="{StaticResource bounty}" TextColor="White" Command="{Binding Path=BindingContext.SelectApartment, Source={x:Reference ApartmentsPage}}" CommandParameter="{Binding .}"/>
    

    Then you will retrieve the single binding context in your Command.

    Thanks indeed I figured it out a bit later, I used the class in my command but I'll try as you say and see if it simplifies the code

    Monday, September 16, 2019 7:16 PM