问题描述
我想通过主键 Label 删除或更新列表视图的行
<Label Grid.Column="1"
Grid.Row="0"
Text="{Binding Label}"
FontSize="20"
TextColor="Black"
Margin="0,10"
FontFamily="Comfortaa-Light"/>
<Label Grid.Column="2"
Grid.Row="1"
Text="{Binding Todo}"
FontSize="20"
TextColor="Black"
Margin="0,10,0"
FontFamily="Comfortaa-Light"
HorizontalOptions="Start"
VerticalOptions="Center"/>
<ImageButton Grid.Column="2"
Grid.Row="0"
HorizontalOptions="End"
VerticalOptions="End"
Source="plus.png"
Margin="9"
BackgroundColor="Transparent"
Clicked="AddReminder"
here parameter{{Binding Label}} />
我需要帮助,谢谢
解决方法
既然你用过数据绑定(MVVM),最好用Command来处理逻辑。此外,我们可以传递标签的文本(或整个模型)而不是标签本身。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App11.MainPage"
x:Name="page"> // set the name of the page
<ImageButton Grid.Column="2"
Grid.Row="0"
HorizontalOptions="End"
VerticalOptions="End"
Source="plus.png"
Margin="9"
BackgroundColor="Transparent"
Command="{Binding Source={x:Reference page},Path=BindingContext.xxxCommand}}"
CommandParameter="{Binding .}"
/>
在您的视图模型中
public ICommand xxxCommand { get; set; }
xxxCommand = new Command((arg)=> {
//var model = arg as YourModel;
// you can get the text of label from the model and do something you want
});