【问题标题】:Xamarin.Forms - Bind Multiple Button Clicks Within Listview Cell?Xamarin.Forms - 在 Listview 单元格中绑定多个按钮单击?
【发布时间】:2016-04-25 13:29:30
【问题描述】:

我想显示一个项目列表,每个项目都包含几个按钮和其他可点击的元素。问题是,默认情况下,Listview 中的每个项目都是可点击的。如何禁用它,并将可点击事件绑定到 Listview 内的元素?

【问题讨论】:

  • 据我所知,listview 只会对不在处理点击的单元格内的元素上的点击做出反应。至少我是这样实现的。
  • 喜欢多选?

标签: listview mvvm xamarin xamarin.forms


【解决方案1】:

您只需要创建一个自定义视图单元格:

public class MyCell : ViewCell
{
    public MyCell ()
    {
        var button1 = new Button {Text = "Button 1"};
        button1.Clicked += (sender1, e1) => {
           // Action for button 1
        };
        var button2 = new Button {Text = "Button 2"};
        button2.Clicked += (sender, e) => {
          // Action for Button 2
        };
        View = new StackLayout
        {
            Orientation = StackOrientation.Horizontal,
            Children = {
                 button1,
                 button2,
            }
        };
        View.GestureRecognizers.Add(new TapGestureRecognizer());
    }
}

最后一个 TapGestureRecognizer 是为了避免触摸的选择(以及在 android 显示上)。

并将其集成到您的 ListView 中

public DemoPage ()
{
    var listView = new ListView
    {
        ItemsSource = new[] {"cell 1", "cell 2", "cell 3", "cell 4"},
        ItemTemplate = new DataTemplate(typeof (MyCell))
    };
    Content = listView;
}

您还可以将按钮点击绑定到命令

【讨论】:

    【解决方案2】:

    如果您使用的是实际的 Button 元素,则不必做任何特别的事情。每个 Button 都会响应它自己的 Clicked 事件。该行的 Tapped 事件应该只在按钮元素之外的区域触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多