Click here to Skip to main content
15,504,549 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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> 

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" }); 

}   

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.
Posted
Updated 29-Nov-17 21:24pm
Comments
Kenneth Haugland 29-Nov-17 8:33am    
Use an IValueConverter and bind this to the expander and a search criteria?
Graeme_Grant 29-Nov-17 10:40am    
Use a CollectionViewSource and apply a filter. You can also group: [^]

UPDATE: "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."

Yes you can. I an see you are binding to the code-behind page, not MVVM. So Create a generic button click event in the code, att a data template to your xaml resources, put a button in the template and link the click event in the xaml to the method in your code behind. Now the sender in the code behind event method is the item from the listview.

1 solution

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; }

    ...
}


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900