Can't update bound property value in command action of list items.

AkihiroAkihiro Member

Hi, guys.
I'd like to toggle select/unselect list item when tapped it
and change enable submit button depending on whether
there is a selected item.

my XAML is following

<ContentPage
    xmlns:viewModel="clr-namespase:MyNameSpace.ViewModel"
    x:Name="MainView"
    x:Class="MyPage">
<ContentPage.BindingContext>
    <viewModel:MyViewModel />
</ContentPage.BindingContext>

<ContentPage.Content>
    <ListView ItemsSource="{Binding Path=Selections, Mode=OneWay}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Frame>
                        <Frame.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binidng Path=BindingContext.SelectCommand, Source={x:Reference Name=MainView}}" CommandParameter="{Binding ., Mode=TwoWay}" />
                        </Frame.GestureRecognisers>
                        <!-- visual element of list item -->
                    </Frame>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Text="Submit!" Command="Binding Path=SubmitCommand}" />
</ContentPage.Content>
</ContentPage>

and when invoking 'SelectCommand', executing following

private void ChangeSelect(ListItemViewModel parameter)
{
    bool selected = !parameter.IsSelected;
    parameter.IsSelected = selected;

    // code to invoking SubmitCommand.CanExecuteChanged event in following
}

this method is normally invoked when item tapped
and SubmitCommand.CanExecute is invoked by CanExecuteChanged event.
but in CanExecute method, "IsSelected' parameter of selected item has not been updated.
what is wrong in my code?

'MyViewModel' class and 'ListItemViewModel' class are implement INotifyPropertyChanged interface.
'MyViewModel.Selections' property is not ObservableCollection but IEnumerable
because it's elements don't add/delete/sort after Initialized.

please tell me a solution.
thanks.

Best Answer

  • AkihiroAkihiro
    Accepted Answer

    @ColeX
    thank you for your reply.
    I can't post complete code because its for our business.
    I had tried remove parts of not-related this problem and run it.
    so, It had run without problem.

    this is not a simple problem with issuing commands and changing properties from list items,
    it seems to be a problem with other parts of my code.
    I'll close this question and try to investigate the cause more.
    I'm grateful for your help.

Answers

  • ColeXColeX Xamurai Member, Xamarin Team Xamurai

    Could you post the complete code or a basic project ? I need to reproduce the issue .

  • AkihiroAkihiro Member
    Accepted Answer

    @ColeX
    thank you for your reply.
    I can't post complete code because its for our business.
    I had tried remove parts of not-related this problem and run it.
    so, It had run without problem.

    this is not a simple problem with issuing commands and changing properties from list items,
    it seems to be a problem with other parts of my code.
    I'll close this question and try to investigate the cause more.
    I'm grateful for your help.

Sign In or Register to comment.