Lubomir Sichta
Lubomir Sichta

Reputation: 1

Xamarin EventToCommand switch toggled

Hello i am trying to use EventToCommand i used prism library but i encountered little problem as described below.

MyTestSwitch_1 works fine executing ToggledCommand as it should but when i try to click on MyTestSwitch_ListView which were generated based on listview items it doesnt do anything doesnt give any exception app does not crash it just ignores EventToCommand behavior

Does anybody know what could be wrong. Thank you.

<StackLayout>
    <Switch x:Name="MyTestSwitch_1" IsToggled="{Binding ValueB}}">
        <Switch.Behaviors >
            <b:EventToCommandBehavior EventName="Toggled" 
                                      Command="{Binding ToggledCommand}"/>
    </Switch.Behaviors>
</Switch>

<ListView x:Name="MyListView"
        ItemsSource="{Binding ValuesList}"
        ItemTapped="Handle_ItemTapped"
        CachingStrategy="RecycleElement"

          HasUnevenRows="True"

        IsPullToRefreshEnabled="True"            
        RefreshCommand="{Binding RefreshCommand}"
        IsRefreshing="{Binding IsRefreshing, Mode=OneWay}"
          Margin="10">
    <ListView.Behaviors>
        <b:EventToCommandBehavior EventName="ItemTapped" 
                                  Command="{Binding ItemTappedCommand}"
                                  CommandParameter="MyParameter" />
    </ListView.Behaviors>

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding Id}" 
               Style="{DynamicResource ListItemTextStyle}" />
                    <Label Text="{Binding Value}" 
               Style="{DynamicResource ListItemDetailTextStyle}"/>
                    <Switch x:Name="MyTestSwitch_ListView" IsToggled="{Binding ValueB}}">
                        <Switch.Behaviors >
                            <b:EventToCommandBehavior EventName="Toggled" 
                                  Command="{Binding  ToggledCommand}, Source={x:Reference ThisPage}"
                                  CommandParameter="{Binding .}" />
                        </Switch.Behaviors>
                    </Switch>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>

Upvotes: 0

Views: 1759

Answers (2)

Aliaksei Aksionau
Aliaksei Aksionau

Reputation: 103

You need to have reference to the ViewModel. As command is its property. Something like this:

Command="{Binding Path=ViewModel.PermissionGroupSelectCommand, Source={x:Reference invitationPage}}"

where Source is referencing the name of the current page and Path referencing command on ViewModel

Upvotes: 0

Lubomir Sichta
Lubomir Sichta

Reputation: 1

Hello i have finally found the solution at this thread

https://forums.xamarin.com/discussion/84190/listview-binding-command

thanks to NMackay

Upvotes: -1

Related Questions