【问题标题】:xamarin Forms - Is it possible to get Grid row selection event?xamarin Forms - 是否可以获得网格行选择事件?
【发布时间】:2017-07-28 21:51:19
【问题描述】:

如何获取网格行选择的行 ID?我想添加行唯一 ID,我希望它被点击或点击。如果有人有任何想法,请建议我。

我想做这样的事情,如果我点击行并且我得到 id。使用此 ID,我将获取另一条记录并在另一个页面上发送用户。为此,我需要行的唯一 ID。

我有如下网格:

下面是我使用的代码:

    public BusList(Common busdata)
    {
        InitializeComponent();
        this.BindingContext = this;
        List<BusDetail> Bus = new List<BusDetail>
        {
            ...
            new BusDetail("Nita Travel","Mumbai", "Navsari",new DateTime(2017, 3, 9), "10:15 AM", "09:15 AM")
        };

        Label header = new Label
        {
            Text = "GridView Demo",
            FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
            HorizontalOptions = LayoutOptions.Center
        };


        var controlGrid = new Grid
        {

            RowSpacing = 2,
            ColumnSpacing = 2,
            Margin = new Thickness(5, Device.OnPlatform(5, 0, 0), 5, 5)
        };
        controlGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(30) });
        for (int i = 0; i < Bus.Count(); i++)
        {
            controlGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(30) });
        }
        controlGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        controlGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        controlGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
       controlGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });



        Bus = Bus.Where(x => x.FromDest.ToLower().Contains(busdata.FromDest.ToLower()) && x.FromDate == busdata.FromDate).ToList();
        controlGrid.Children.Add(new Label { Text = "ID", Font = Font.SystemFontOfSize(NamedSize.Large).WithAttributes(FontAttributes.Bold), BackgroundColor = Color.Gray }, 0, 0);
        controlGrid.Children.Add(new Label { Text = "Travel Name", Font = Font.SystemFontOfSize(NamedSize.Large).WithAttributes(FontAttributes.Bold), BackgroundColor = Color.Gray }, 1, 0);
        controlGrid.Children.Add(new Label { Text = "Arr Time", Font = Font.SystemFontOfSize(NamedSize.Large).WithAttributes(FontAttributes.Bold), BackgroundColor = Color.Gray }, 2, 0);
        controlGrid.Children.Add(new Label { Text = "Dep Time", Font = Font.SystemFontOfSize(NamedSize.Large).WithAttributes(FontAttributes.Bold), BackgroundColor = Color.Gray }, 3, 0);


        for (int i = 0; i < Bus.Count(); i++)
        {
            var clickableRow = new ContentView();

            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, "RowTappedCommand");
            tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, i.ToString()); 

            clickableRow.GestureRecognizers.Add(tapGestureRecognizer);
            clickableRow.BindingContext = i.ToString();
            controlGrid.Children.Add(clickableRow, 0, i);
            Grid.SetColumnSpan(clickableRow, 3);


            controlGrid.Children.Add(new Label { Text = Bus[i].TravelName, BindingContext = Bus[i].TravelName, BackgroundColor = Color.LightGray, VerticalTextAlignment = TextAlignment.Center }, 1, i + 1);
            controlGrid.Children.Add(new Label { Text = Bus[i].ArrivalTime, BindingContext = Bus[i].ArrivalTime, BackgroundColor = Color.LightGray, VerticalTextAlignment = TextAlignment.Center }, 2, i + 1);
            controlGrid.Children.Add(new Label { Text = Bus[i].DepartureTime, BindingContext= Bus[i].DepartureTime, BackgroundColor = Color.LightGray, VerticalTextAlignment = TextAlignment.Center }, 3, i + 1);

        }
        this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
        this.Content = new StackLayout
        {
            Children =
            {
                header,
                controlGrid
            }
        };
    }

  public  void RowTappedCommand(string index)
    {
        var tappedRow = int.Parse(index);

    }

【问题讨论】:

    标签: xamarin grid xamarin.forms


    【解决方案1】:

    您可以将ContentView 添加到第一列的每一行,并使用ColumnSpan 属性将其跨越所有列。然后你会用TouchGestureListener 处理水龙头。创建Command 时,您还需要包含CommandParameter,其中包含相关行的索引。

    在构造函数之外,定义这个:

    public ICommand RowTappedCommand { get; private set; }
    

    然后在InitializeComponent后面某处添加如下代码:

    RowTappedCommand = new Command<string>(RowTapped);
    

    然后为每一行创建可点击的 ContentView 控件:

    var clickableRow = new ContentView {
        HorizontalOptions = LayoutOptions.FillAndExpand,
        VerticalOptions = LayoutOptions.FillAndExpand
    }
    
    var tapGestureRecognizer = new TapGestureRecognizer();
    tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, "RowTappedCommand");
    var binding = new Binding();
    binding.Source = i.ToString();
    tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, binding); // This is the index of the row
    clickableRow.GestureRecognizers.Add(tapGestureRecognizer);
    
    controlGrid.Children.Add(clickableRow, 0, 0);
    Grid.SetColumnSpan(clickableRow,3);
    

    您还需要定义与CommandProperty 中定义的名称匹配的方法。如果您为视图定义了视图模型,则需要在那里进行。否则,您需要将该方法添加到视图的代码隐藏文件 (xaml.cs) 中。

    void RowTapped(string index)
    {
        var tappedRow = int.Parse(index);
        // Do something with the value
    }
    

    请记住正确设置 BindingContext 以便调用您的命令。以下是有关 Xamarin.Forms 中的命令的更多信息:


    更新:有一些错误需要修正。

    1. 命令定义不正确。
    2. 命令属性的绑定需要指向一个静态值

    Simplifying Events with Commanding

    【讨论】:

    • 感谢@hankide 的回复,我还有一个问题是如何使用它?我必须做任何方法??对不起我的问题,但我是 xamarin 的新手,所以请帮助我。
    • 我在 .cs 文件中添加了这段代码,但它不起作用。我没有在 RowTappedCommand 方法中接到电话。请帮我解决这个问题。
    • @Dhruti 你设置了绑定上下文吗?
    • @Dhruti 如果您的所有代码都在代码隐藏文件中,请将其放在构造函数中 InitializeComponent 之后:this.BindingContext = this。这样就可以正确找到并调用该命令。
    • 是的,我已经设置好了。设置绑定内容是否正确? controlGrid.Children.Add(new Label { Text = Bus[i].TravelName, BindingContext = Bus[i].TravelName, BackgroundColor = Color.LightGray, VerticalTextAlignment = TextAlignment.Center }, 1,1);
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2021-05-18
    • 1970-01-01
    • 2021-01-03
    • 2018-04-12
    相关资源
    最近更新 更多