lilcodelab / Xamarin.Plugin.Calendar

Calendar plugin for Xamarin.Forms
MIT License
260 stars 60 forks source link

Item click #15

Closed JanettHolst290490 closed 5 years ago

JanettHolst290490 commented 5 years ago

Is it possible to click on an item in the list below and react on it ? I can't seem to find any examples on this. Something like this doesn't work:

<DataTemplate>
        <StackLayout
            Padding="15,0,0,0">
            <StackLayout.GestureRecognizers>
                <TapGestureRecognizer
                    Command="{Binding TapCommand}" />
            </StackLayout.GestureRecognizers>
            <Label
                Text="{Binding Name}"
                FontAttributes="Bold"
                FontSize="Medium" />
            <Label
                Text="{Binding Description}"
                FontSize="Small"
                LineBreakMode="WordWrap" />
        </StackLayout>
    </DataTemplate>

How do I make an item clickable in the list in the bottom?

PenguinPero commented 5 years ago

Something like this should work:

In the XAML:

<ContentPage
...
x:Name="myPage">
...
<TapGestureRecognizer
    Command="{Binding BindingContext.TapCommand, Source={x:Reference myPage}}"
    CommandParameter="{Binding .}" />

In your view model:

public MyViewModel()
{
    TapCommand = new Command<MyUIModel>(TapCommandExecute);
}

public ICommand TapCommand { get; }

private void TapCommandExecute(MyUIModel param)
{
    // your logic
}

Explanation: Command="{Binding BindingContext.TapCommand, Source={x:Reference myPage}}" As a source you take your myPage object which is your Page Page objects have BindingContext which should be your MyViewModel, so BindingContext.TapCommand will point to your command on your VM

CommandParameter="{Binding .}" will send the current BindingContext of the item on the list as a parameter to the command, so you need to make sure your command is a generic version with <T>, where T is your model you bind to the items, example new Command<MyUIModel>(TapCommandExecute)

Hope it helps :)

BrunoFSimon commented 5 years ago

I had same problem @JanettHolst290490

I solved using TapCommand = new Command(TapCommandExecute); instead of TapCommand = new Command<MyUIModel>(TapCommandExecute); and on my xaml <TapGestureRecognizer Command="{Binding BindingContext.TapCommand, Source={x:Reference MYPAGE}}" CommandParameter="{Binding .}" />

PenguinPero commented 5 years ago

@JanettHolst290490 did you make it work? can we close the issue?

JanettHolst290490 commented 5 years ago

Yes I made it work. However I'm using FreshMvvm so the bindings are a bit different. But I resolved the issue by setting the Command on the Event Item's View model.

Thank you for the help, you guided me in the right direction.