单击行时将动态值传递给事件处理程序 [英] Passing a dynamic value to an event handler when a row is clicked

查看:35
本文介绍了单击行时将动态值传递给事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App1"
             x:Class="App1.MainPage">

    <ContentPage.BindingContext>
        <local:PersonViewModel />
    </ContentPage.BindingContext>

    <StackLayout>
        <ListView x:Name="listView" ItemsSource="{Binding People}">
            <ListView.Header>
                <Grid>
                    <Grid.BindingContext>
                        <local:PersonViewModel />
                    </Grid.BindingContext>


                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*"  />
                        <ColumnDefinition Width="*"  />
                        <ColumnDefinition Width="*"  />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Label Grid.Column="1" Text="Id" />
                    <Label Grid.Column="2" Text="First Name" />
                    <Label Grid.Column="3" Text="Surname" />
                    <Label Grid.Column="4" Text="Date Of Birth" />
                </Grid>
            </ListView.Header>

        <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*"  />
                                <ColumnDefinition Width="*"  />
                                <ColumnDefinition Width="*"  />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Label Grid.Column="1" Text="{Binding Id}" />
                            <Label Grid.Column="2" Text="{Binding FirstName}" />
                            <Label Grid.Column="3" Text="{Binding Surname}" />
                            <Label Grid.Column="4" Text="{Binding Age}" />
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

和下面的视图模型:

public class PersonViewModel
    {
        private ObservableCollection<Person> _people = new ObservableCollection<Person>();

        public ObservableCollection<Person> People
        {
            get { return _people; }
        }

        public PersonViewModel()
        {
            _people.Add(new Person { Id = Guid.NewGuid().ToString(), Age = 36, FirstName = "Andrew", Surname = "Smith" });
            _people.Add(new Person { Id = Guid.NewGuid().ToString(), Age = 65, FirstName = "David", Surname = "White" });
            _people.Add(new Person { Id = Guid.NewGuid().ToString(), Age = 39, FirstName = "Bert", Surname = "Edwards" });
        }
    }

它按预期工作,即将网格添加到页面中.如果用户单击其中一行;我希望将该行的 ID 传递给事件处理程序.我该怎么做呢.我花了过去三个小时研究这个,但是我没有找到答案.我找到的最接近的是:https://forums.xamarin.com/discussion/23002/how-do-you-detect-a-grid-click-tap.但是,在这种情况下,传递的是静态值而不是动态值.

It works as expected i.e. the grid is added to the page. If a user clicks on one of the rows; I want the ID of that row to be passed to an event handler. How do I do this. I have spent the last three hours researching this, however I have not found an answer. The closest I have found is this: https://forums.xamarin.com/discussion/23002/how-do-you-detect-a-grid-click-tap. However, in this case a static value is passed and not a dynamic value.

推荐答案

正如Jason所说,ListView中有selecteditem属性,类型为Person模型,可以在PersonViewmodel中创建person模型,绑定到ListView SelectedItem并实现INotifyPropertyChanged单击 ListView 项时更新.

As Jason said, there is selecteditem property in ListView, the type is Person model, you can create person model in PersonViewmodel, binding to ListView SelectedItem and implement INotifyPropertyChanged to update when you click ListView Item.

 <ListView
            x:Name="listView"
            ItemsSource="{Binding peoples}"
            SelectedItem="{Binding selecteditem}">
            <ListView.Header>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Label Grid.Column="0" Text="Id" />
                    <Label Grid.Column="1" Text="First Name" />
                    <Label Grid.Column="2" Text="Surname" />
                    <Label Grid.Column="3" Text="Date Of Birth" />
                </Grid>
            </ListView.Header>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Label Grid.Column="0" Text="{Binding Id}" />
                            <Label Grid.Column="1" Text="{Binding FirstName}" />
                            <Label Grid.Column="2" Text="{Binding Surname}" />
                            <Label Grid.Column="3" Text="{Binding Age}" />
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

public class PersonViewModel:ViewModelBase
{
    public ObservableCollection<personmodel> peoples { get; set; }

    private personmodel _selecteditem;
    public personmodel selecteditem
    {
        get { return _selecteditem; }
        set
        {
            _selecteditem = value;
            RaisePropertyChanged("selecteditem");
        }
    }     
    public PersonViewModel()
    {
        peoples = new ObservableCollection<personmodel>();
        peoples.Add(new personmodel { Id = Guid.NewGuid().ToString(), Age = 36, FirstName = "Andrew", Surname = "Smith" });
        peoples.Add(new personmodel { Id = Guid.NewGuid().ToString(), Age = 65, FirstName = "David", Surname = "White" });
        peoples.Add(new personmodel { Id = Guid.NewGuid().ToString(), Age = 39, FirstName = "Bert", Surname = "Edwards" });
        selecteditem = peoples[0];
    }
}


public class ViewModelBase: INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

因此,如果您想将单击的行 ID 传递给事件处理程序,只需传递

So if you want to pass clicked row Id to event handler, you just pass

selecteditem.Id

这篇关于单击行时将动态值传递给事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆