更新ListView项目中的值

时间:2018-02-09 07:01:33

标签: c# xml listview xamarin xamarin.forms

我有一个帖子的ListView,我需要在点击时更改类似的按钮图像源。

我的XML是

<ListView x:Name="MessageView" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout BackgroundColor="White" Margin="10, 10, 10, 0" Padding="10, 10, 15, 10">
                            <Image Source="options_icon.png" HeightRequest="15" HorizontalOptions="End" Margin="0, 0, 10, 0">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding LikeClick}"/>
                                </Image.GestureRecognizers>
                            </Image>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

绑定
LikeClick = new Command(() => LikeMessage(navigation, message.Id)),

LikeMessage方法是

public static async Task LikeMessage(INavigation navigation, int id)
    {
*Web Request*


        if (page_result.Equals("liked"))
        {
           //Update source to "liked_icon.png"
        }

        if (page_result.Equals("unliked"))
        {
            //Update source to "like_icon.png"
        }
    }

2 个答案:

答案 0 :(得分:1)

首先为您的<StackLayout>元素命名,

<StackLayout x:Name="Item" BackgroundColor="White" Margin="10, 10, 10, 0" Padding="10, 10, 15, 10">

其次,在列表中,点击手势应如下所示

 <TapGestureRecognizer BindingContext="{Binding Source={x:Reference MessageView}, Path=BindingContext}"   Command="{Binding LikeClick}" CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"/>

第三,绑定你的图像源,

     <Image Source="{Binding ImageSource}" HeightRequest="15" HorizontalOptions="End" Margin="0, 0, 10, 0">

Forth,在您的viewmodel类中将ImageSource设置为您想要更改的源。

答案 1 :(得分:1)

您需要将INotifyPropertyChanged实施到您的Rates课程,并执行以下操作

更新课程

Me.Close()

命令绑定应该是这样的:

MessageObject

绑定图像源

public string _Source;
public string Source
{
    get { return _Source; }
    set
    {
        _Source = value;
        OnPropertyChanged(nameof(Source));
    }
}

最后在LikeMessage()中设置

LikeClick = new Command(() => LikeMessage(navigation, message)),