【问题标题】:How to detect clickable label position inside listview xamarin forms如何检测listview xamarin表单中的可点击标签位置
【发布时间】:2018-05-07 10:49:37
【问题描述】:

我正在尝试做这样的事情

Like This

我有Listview 并点击了label“编辑”我想要当我点击这个标签时,它的位置被检测到并显示

点击 Aliaddress 上的标签

我为此使用了TapGestureRecognizer,但是当我用谷歌搜索时,我发现所选项目不适用于 TapGesture

这是我的 xaml

<ListView ItemsSource="{Binding UserAdresses}" SelectedItem="{Binding SelectedAddress}" HorizontalOptions="{Binding HoriRLLR}" RowHeight="{Binding RowHeight}" VerticalOptions="FillAndExpand">
                            <ListView.ItemTemplate>
                                <DataTemplate>

                                    <ViewCell>
                                        <StackLayout VerticalOptions="FillAndExpand">
                                            <Label Text="{Binding Country}"  TextColor="Orange" FontSize="Large" FontAttributes="Bold"></Label>

                                            <Label Text="{Binding Address}"  TextColor="Black" FontSize="Medium"></Label>

                                            <Label Text="{Binding City}" TextColor="Black" FontSize="Medium"></Label>

                                            <Label Text="{translator:Translate Edit}" TextColor="Black" FontSize="Medium">

                                                <Label.GestureRecognizers>
                                                    <TapGestureRecognizer
                                                    Command="{Binding Path=BindingContext.EditAddressesCommand,  Source={x:Reference CustomerAdressesPage}}"/>
                                                </Label.GestureRecognizers>
                                                <Label.Effects>
                                                    <controls:UnderlineEffect></controls:UnderlineEffect>
                                                </Label.Effects>

                                            </Label>
                                        </StackLayout>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

我的代码

 public DelegateCommand EditAddressesCommand => new DelegateCommand(EditAddresses); 
        public DelegateCommand DeleteAddressesCommand => new DelegateCommand(DeleteAddresses);
        private readonly IPageDialogService _dialogService;

        private ObservableCollection<CustomerAdressesModel> _userAdresses;
        public ObservableCollection<CustomerAdressesModel> UserAdresses
        {
            get { return _userAdresses; }
            set { SetProperty(ref _userAdresses, value);
            }
        }


        private CustomerAdressesModel _selectedAddress;
        public CustomerAdressesModel SelectedAddress
        {
            get { return _selectedAddress; }
            set { SetProperty(ref _selectedAddress, value); }
        }


        private void EditAddresses()
        {
            _dialogService.DisplayAlertAsync("Test", "Edit Clicked", "Ok");
        }

我怎样才能做到这一点并检测点击标签的位置

【问题讨论】:

  • 使用CommandParameter绑定当前对象“.”
  • "。"与我的模型或什么有关?

标签: mvvm xamarin.forms prism


【解决方案1】:

你可以在TapGestureRecognizer里面使用这个:CommandParameter="{Binding .}"

Xaml:

 <Label.GestureRecognizers>
   <TapGestureRecognizer
           CommandParameter="{Binding .}"
           Command="{Binding Path=BindingContext.EditAddressesCommand,  Source={x:Reference CustomerAdressesPage}}"/>
 </Label.GestureRecognizers>

视图模型:

 public ICommand EditAddressesCommand
    {
        get
        {
            return new Command<YourModel>((YourModel model) =>
            {
                //Access your model properties
            });
        }
    }

希望这可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    相关资源
    最近更新 更多