在xaml中查找listview的特定项目? [英] Find the specific item of a listview in xaml?

查看:74
本文介绍了在xaml中查找listview的特定项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有listview。

Say I have the listview.

<ListView Margin="10" Name="lvUsers" ItemsSource="{Binding People}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
            <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
        </GridView>
    </ListView.View>
</ListView> 



在ViewMOdel中


In ViewMOdel

public ObservableCollection<Person> People { get; set; }

public ListView()
{
    InitializeComponent();

    this.People = new ObservableCollection<Person>();
    this.People.Add(new Person() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" });
    this.People.Add(new Person() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
    this.People.Add(new Person() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe@gmail.com" }); 

}   



这只是一个演示。在我的真实代码中,我在项目的左侧有扩展器。我跳过它,因为它是长度代码。现在我想添加一个controltemplate或按钮。调用它会找到一个特定的项目来扩展它。我知道如果你使用这个这个



但我们可以在xaml中完成吗?我的意思是在controltemplate中添加事件或触发一些东西?



我尝试过的事情:



不知道怎么做,需要代码。


It is just a demo. In my real code I have expanders on the left of items. I skip it as it is length code. Now I want to add a controltemplate or button something. Invoke it will find a specific item to expand it. I know it may easy to do in code behind if you use this or this.

But can we do it in xaml? I mean add event or trigger something in controltemplate?

What I have tried:

Not sure how to do it, need code.

推荐答案

两个答案:



1.如果您要根据UI选择进行过滤,那么CollectionViewSource就像Graeme_Grant所提到的那样。



2.如果你是那个人'正在寻找是与人的现有关系,那么最合适的实现是修改PersonViewModel以包含对该人的引用。



Two answers:

1. If you're going to be doing a filter based on UI selection then the CollectionViewSource is the way to go as Graeme_Grant mentioned.

2. If the person you're looking for is an existing relationship to the person, then the most appropriate implementation is modifying the PersonViewModel to contain references to that person.

public class PersonViewModel
{
    public PersonViewModel EmergencyContact { get; set; }

    ...
}





然后将按钮绑定到命令并传递通过CommandParameter获得所需的属性。



Then bind the button to a command and pass the desired property via CommandParameter.


这篇关于在xaml中查找listview的特定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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