绑定上下文更改的列表视图清空按钮文本

时间:2018-04-09 09:55:42

标签: xamarin xamarin.forms freshmvvm

我一直面对这种奇怪的行为,我现在无法理解导致问题的原因。

我正在使用FreshMvvm,我有一个ListView,里面有两个按钮。

现在问题是其中一个按钮从Binding获取其文本,其次是通过单击命令分配按钮我必须遵循this

现在添加了这个之后,click事件工作得很好,但文本绑定不起作用我怀疑这是因为绑定上下文更改发生了我确定是完整的原因但是我无法找到解决这个问题的方法listview代码如下:

<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>

问题在于带有文本绑定的按钮,即使数据实际存在,文本只显示为空。

Click事件代码如下:

public ICommand GoFeatureActivityDetail { get; set; }

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

private async void FeatureActivityDetailCommand(object obj)
{}

1 个答案:

答案 0 :(得分:1)

正确的思考,糟糕的实施。

这里发生的事情是你已经更改了按钮的BindingContext,现在它不再能够“看到”该项目的ActivityAmount属性,因为它正在“寻找”BrowseFeaturesPageModel 1}}对象。 你可以保持简单,只改变BindingContext你要使用的地方,而不是整个View(在这种情况下是按钮):

<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 .}"/>