简体   繁体   English

Xamarin Forms ListView ItemTapped to object from ViewModel

[英]Xamarin Forms ListView ItemTapped to object from ViewModel

I have a problem.我有个问题。 I have created a ListView with an ItemSource from my ViewModel.我已经从我的 ViewModel 中创建了一个带有 ItemSource 的 ListView。 Now the ItemSource is an ObservableCollection<KnownDevice> with a few objects called KnownDevice .现在 ItemSource 是一个ObservableCollection<KnownDevice>其中包含一些名为KnownDevice对象。 In my ListView I have set: ItemTapped="rowDevice_Clicked" with the following function:在我的 ListView 中,我设置了: ItemTapped="rowDevice_Clicked"具有以下功能:

private void rowDevice_Clicked(object sender, ItemTappedEventArgs e)
{
    ListView listView= (ListView)sender;
}

But now I need to know which KnownDevice was clicked.但是现在我需要知道点击了哪个KnownDevice。

How can I do that?我怎样才能做到这一点?

just make another single instance of your KnownDevice只需创建另一个已知设备实例

public ObservableCollection<KnownDevice> ThisIsMySelectionList {get; set;}
public KnownDevice ThisIsTheSelectedDeviceFromList {get; set;}

Then, in your list definition然后,在您的列表定义中

<ListView 
   ItemsSource="{Binding ThisIsMySelectionList}"
   SelectedItem="{Binding ThisIsTheSelectedDeviceFromList}" >

   .. rest of your column definitions to show

</ListView>

As an item is tapped/selected from the list, it is bound to the public get/set of "ThisIsTheSelectedDeviceFromList" as bound to the SelectedItem value当从列表中点击/选择一个项目时,它会绑定到“ThisIsTheSelectedDeviceFromList”的公共获取/设置,因为它绑定到 SelectedItem 值

I'm sure there are others who can point to a more direct way, but within your getter/setter, you can point to the app object yourself, such as我敢肯定还有其他人可以指向更直接的方式,但是在您的 getter/setter 中,您可以自己指向 app 对象,例如

public KnownDevice ThisIsTheSelectedDeviceFromList 
{
   get { return ((YourNamespace.App)App).YourKnownDeviceProperty; }
   set {((YourNamespace.App)App).YourKnownDeviceProperty = value; }
}

So here, I am forcing a typecast to specifically YOUR App by qualifying your project namespace.app of the static app object, then your property.所以在这里,我通过限定静态应用程序对象的项目 namespace.app,然后是您的属性,强制对您的应用程序进行类型转换。 You can get and set directly as needed.您可以根据需要直接获取和设置。

rowDevice_Clicked 中添加这行代码

var device = e.Item as KnownDevice

You need to create a property selectedDevice and bind that to SelectedItem of listview, below is my code hope it will be helpful for you.您需要创建一个属性 selectedDevice 并将其绑定到列表视图的 SelectedItem,下面是我的代码,希望对您有所帮助。

ListView XAML :列表视图 XAML :

<ListView HasUnevenRows="True" 
          ItemsSource="{Binding KnownDeviceItems}" 
          SelectedItem="{Binding SelectedKnownDevice}"
          SeparatorVisibility="None">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    //code here for cell
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

ViewModel Properties :视图模型属性:

private ObservableCollection<KnownDevice> knownDeviceItems;
    public ObservableCollection<KnownDevice> KnownDeviceItems
    {
        get { return knownDeviceItems; }
        set
        {
            knownDeviceItems = value;
            OnPropertyChanged();
        }
    }

    private KnownDevice selectedKnownDevice;
    public KnownDevice SelectedKnownDevice
    {
        get { return selectedKnownDevice; }
        set
        {
            selectedKnownDevice = value;
            OnPropertyChanged();
        }
    }

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM