微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我想在 xaml 中获取带按钮的参数 在您的视图模型中

如何解决我想在 xaml 中获取带按钮的参数 在您的视图模型中

我想通过主键 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 
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。