Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Problem with triggers in listview

ABloxityABloxity Member ✭✭✭

I am using button triggers within a list of posts, I click on it and I like in both posts I like it,

I just want where I click to paint without affecting the other.

Suggestions?

Answers

  • igorkr_10igorkr_10 Member ✭✭✭✭

    Why don't you want to create custom reusable control based on ContentView? Why don't you want to implement normal binding in that control? Why do you find wrong ways instead of implementing true'MVVM?

  • ABloxityABloxity Member ✭✭✭

    My triggers are Boolean, but here I select one and I change the others (in style).

    I already searched, I found, of the examples they have done, but the same result comes out.

    And everything is done in the viewModel

  • igorkr_10igorkr_10 Member ✭✭✭✭

    Can you show code where you store the style for clicked MeGusta?

  • ABloxityABloxity Member ✭✭✭

    @igorkr_10

    These are my values ​​to change the styles in the trigger button

    in my viewmodel

    and in the button command LikePublicacion

    I do the conditions where I click the button brings me the id item of the publication

    and the other if the trigger is the same or different from the Boolean value, paint me only the one that is clicked, but pass it on and all the other also

  • igorkr_10igorkr_10 Member ✭✭✭✭
    edited October 2019

    What is ListPublicacionesPerfil? And your command was not called?

  • ABloxityABloxity Member ✭✭✭

    It is the reference of the name of the list.
    to be able to bindear the button it indicated to me that I need to bindear the button making reference within the listview

  • igorkr_10igorkr_10 Member ✭✭✭✭

    @ABloxity said:

    It is the reference of the name of the list.
    to be able to bindear the button it indicated to me that I need to bindear the button making reference within the listview

    OK. You button allocated in ITEM of ListView. Item has its owns BindingContext.
    So your code must be:

    public class ListViewModel
    {
         public ObservableCollection<ListItemViewModel> Items { get; }
    }
    
    public class ListItemViewModel
    {
        public bool Liked { get; set; }
        public Command LikePublication { get; }
    }
    

    ListView can see Items. But ITEM in that ListView is ListItemViewModel. So item in ListView can't see its parent - ListViewModel. I mean the BindingContext in Button cannot be that collection because button can only see ListItemViewModel, not ListViewModel

  • ABloxityABloxity Member ✭✭✭

    @igorkr_10 said:

    OK. You button allocated in ITEM of ListView. Item has its owns BindingContext.
    So your code must be:

    public class ListViewModel
    {
       public ObservableCollection<ListItemViewModel> Items { get; }
    }
    
    public class ListItemViewModel
    {
      public bool Liked { get; set; }
      public Command LikePublication { get; }
    }
    

    ListView can see Items. But ITEM in that ListView is ListItemViewModel. So item in ListView can't see its parent - ListViewModel. I mean the BindingContext in Button cannot be that collection because button can only see ListItemViewModel, not ListViewModel

    So, the logic I have, would it be wrong? or I only add (what I mention) in how it should look

  • ABloxityABloxity Member ✭✭✭

    @igorkr_10 said:
    then, it would work if I click a button, I wouldn't have to affect the other

  • LandLuLandLu Member, Xamarin Team Xamurai

    I guess you want to bind your button's command to the method which aligns in your view model. There's no need to reset your button's binding context in the list view. And it should not be changed or the source will be messed up.
    Use source keyword to change it like:

    // Remove the binding context's assigning
    Command="{Binding BindingContext.LikePublicacion, Source={x:Reference ListPublicacionesPerfil}}" 
    CommandParameter="{Binding}"
    

    Moreover, try to post your code directly instead of using the image. Then we could copy them to reproduce it on my side.

  • ABloxityABloxity Member ✭✭✭

    @LandLu said:
    I guess you want to bind your button's command to the method which aligns in your view model. There's no need to reset your button's binding context in the list view. And it should not be changed or the source will be messed up.
    Use source keyword to change it like:

    // Remove the binding context's assigning
    Command="{Binding BindingContext.LikePublicacion, Source={x:Reference ListPublicacionesPerfil}}"
    CommandParameter="{Binding}"
    Moreover, try to post your code directly instead of using the image. Then we could copy them to reproduce it on my side.

    This its

  • LandLuLandLu Member, Xamarin Team Xamurai

    When you want to consume another source outside of your current binding context for a single property there's no need to change the whole control's binding context. This will affect other properties' binding. Use source to make it:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/binding-path#debugging-complex-paths
    I think each item needs an IsLiked property to point out the status. So IsLiked should be embedded in the Publicaciones class in your case. And as we have changed the source to your list view, the LikePublicacion command should be populated in the publicacionesViewModel class like what you've done for HacerPublicacion, Comment and EdirOrDelete.

    The class ListViewModel and ListItemViewModel you defined in the view model won't be called because you didn't consume them in the XAML.

  • ABloxityABloxity Member ✭✭✭

    @LandLu said:
    When you want to consume another source outside of your current binding context for a single property there's no need to change the whole control's binding context. This will affect other properties' binding. Use source to make it:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/binding-path#debugging-complex-paths
    I think each item needs an IsLiked property to point out the status. So IsLiked should be embedded in the Publicaciones class in your case. And as we have changed the source to your list view, the LikePublicacion command should be populated in the publicacionesViewModel class like what you've done for HacerPublicacion, Comment and EdirOrDelete.

    The class ListViewModel and ListItemViewModel you defined in the view model won't be called because you didn't consume them in the XAML.

    And the classes like the ones in the XAML?

  • LandLuLandLu Member, Xamarin Team Xamurai

    I don't mean the classes. There's no need to define an extra class in your view model.
    Define LikePublicacion directly in it just like your other commands and adjust the binding in the XAML like:

    Command="{Binding BindingContext.LikePublicacion, Source={x:Reference ListPublicacionesPerfil}}" 
    CommandParameter="{Binding}"
    
Sign In or Register to comment.