1

I have been facing this weird behaviour for a while now and I am unable to understand what exactly is causing the problem.

I am using FreshMvvm and I have a ListView with two buttons inside of it.

Now the problem is one of the buttons gets its text from Binding, Secondly to assign the button with a click command I had to follow this.

Now after adding this, the click event works perfectly but the text binding is not working I suspected this happened because of binding context change which I am sure is the whole reason but I am not able to find a way to fix this my listview code is as follows:

<ListView Grid.Row="1" 
          ItemsSource="{Binding CategoryAndActivities}" 
          x:Name="WishListName"
          HorizontalOptions="FillAndExpand" 
          VerticalOptions="FillAndExpand" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>                                        
                    <Frame>
                        <Grid VerticalOptions="FillAndExpand" 
                              HorizontalOptions="FillAndExpand">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <!--Image and Title-->
                            <AbsoluteLayout Grid.Row="0"
                                            HeightRequest="70" 
                                            IsClippedToBounds = "true">
                                <ffimageloading:CachedImage Source="{Binding ActivityImage}" 
                                                            Aspect="AspectFill" 
                                                            AbsoluteLayout.LayoutFlags="All"
                                                            AbsoluteLayout.LayoutBounds="0.0, 0.0, 0.3, 0.85" 
                                                            Margin="5, 0, 5, 5" 
                                                            ErrorPlaceholder="nopreviewlandscape" 
                                                            LoadingPlaceholder="loadingicon"/>
                                <Label x:Name="ActivityNameLabel" 
                                       Text="{Binding ActivityName}" 
                                       FontAttributes="Bold" 
                                       VerticalTextAlignment="Start"
                                       TextColor="{StaticResource price_text_color}" 
                                       FontSize="Small"
                                       AbsoluteLayout.LayoutFlags="All"
                                       AbsoluteLayout.LayoutBounds="1.0, 0.0, 0.7, 0.85" 
                                       Margin="5, 5, 5, 5">
                                </Label>
                            </AbsoluteLayout>
                            <!--Descp-->
                            <StackLayout Grid.Row = "1" 
                                         IsClippedToBounds="true">
                                <Label Text="{Binding AcitivityDescription}" 
                                       FontSize="Small" 
                                       LineBreakMode="WordWrap" 
                                       Margin="5, 0, 5, 5"/>
                            </StackLayout>
                            <Grid BackgroundColor="White" 
                                  Grid.Row = "2" 
                                  VerticalOptions="FillAndExpand" 
                                  HorizontalOptions="FillAndExpand">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="50*"/>
                                    <ColumnDefinition Width="50*"/>
                                </Grid.ColumnDefinitions>

                                <Button BackgroundColor="{StaticResource ColorBrandingYellow}"  
                                        HorizontalOptions="FillAndExpand"  
                                        Command="{Binding AddToWishListCommand}" 
                                        Grid.Column="0" 
                                        BindingContext="{Binding Source={x:Reference ListName}, Path=BindingContext}" 
                                        CommandParameter="{Binding Source={x:Reference ActivityNameLabel},Path=BindingContext}" 
                                        TextColor="Black" 
                                        Text="{resourceLocal:Translate addToWishlist}" 
                                        FontSize = "Small" />
                                <Button BackgroundColor="{StaticResource ColorBrandingYellow}" 
                                        HorizontalOptions="FillAndExpand" 
                                        Grid.Column="1" 
                                        TextColor="Black" 
                                        Text="{Binding ActivityAmount}" 
                                        FontSize = "Small" 
                                        Command="{Binding GoFeatureActivityDetail}" 
                                        BindingContext="{Binding Source={x:Reference ListName}, Path=BindingContext}"
                                        CommandParameter="{Binding Source={x:Reference ActivityNameLabel},Path=BindingContext}"/> 
                            </Grid>
                        </Grid>
                    </Frame>                                        
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

The problem is in the button with binding as text what happems is the text just show empty even though the data actually exist.

The Click event code is as follows:

public ICommand GoFeatureActivityDetail { get; set; }

public BrowseFeaturesPageModel()
{
    AddToWishListCommand = new Command(WishListCommand);
    GoFeatureActivityDetail = new Command(FeatureActivityDetailCommand);
}

private async void FeatureActivityDetailCommand(object obj)
{}
4

1 回答 1

1

Right thinking, bad implementation.

What's going on here is that you've changed the Button's BindingContext and now it's not anymore able to "see" the ActivityAmount property of the item 'cause it's 'looking' to the BrowseFeaturesPageModel object. You can keep the things simpler, changing the BindingContext only where you'll use, not the whole View (the button in this case):

<Button BackgroundColor="{StaticResource ColorBrandingYellow}" 
        HorizontalOptions="FillAndExpand" 
        Grid.Column="1" 
        TextColor="Black" 
        Text="{Binding ActivityAmount}" 
        FontSize = "Small" 
        Command="{Binding BindingContext.GoFeatureActivityDetail, Source={x:Reference ListName}}" 
        CommandParameter="{Binding .}"/> 
于 2018-04-09T12:14:55.547 回答