Problem with traversing WPF element tree

I have a ListBox data bound to the collection of my PersonCollection class. Next, I have defined a data template for objects of type Person, consisted of a DockPanel which contains a TextBlock for a person's name and a Button to remove the person from the list. It looks very nice all together.

The problem I am facing is that I am unable to reach the selected item (and delete it) in the list box when I click the button defined in the data template. Here's the handler of the button:

private void RemovePersonButton_Click(object sender, RoutedEventArgs e)
{
    Button clickedButton = (Button)e.Source;
    DockPanel buttonPanel = (DockPanel)clickedButton.Parent;
    Control control = (Control)button.Parent;
}

The last created object control is null, i.e. I cannot progress further up the element tree, so I cannot reach the list and its SelectedItem. Important thing to note here is that cannot simply go with getting the selected item from the list by calling it, because I have more than one list in the window and all those lists implement the same data template, i.e. share the same event handler for the delete button.

I would appreciate all the help I could get. Thanks.

Answers


If I understand the question correctly I think you'll be able to get the Person from the Button's DataContext

private void RemovePersonButton_Click(object sender, RoutedEventArgs e) 
{
    Button clickedButton = (Button)e.Source; 
    Person selectedItem = clickedButton.DataContext as Person;
    if (selectedItem != null)
    {
        PersonCollection.Remove(selectedItem);
    }
}

Another way is to find the ListBox in the VisualTree

private void RemovePersonButton_Click(object sender, RoutedEventArgs e) 
{
    Button clickedButton = (Button)e.Source; 
    ListBox listBoxParent = GetVisualParent<ListBox>(clickedButton );
    Person selectedItem = listBoxParent.SelectedItem as Person;
    //...
}

public T GetVisualParent<T>(object childObject) where T : Visual
{
    DependencyObject child = childObject as DependencyObject;
    while ((child != null) && !(child is T))
    {
        child = VisualTreeHelper.GetParent(child);
    }
    return child as T;
}

Need Your Help

Get active window without global hooks or polling GetActiveWindow?

c# c++ windows winapi

How can I get notifications about what is the currect active window and when this changes without polling GetActiveWindow or using global hooks?

WCF Exposing Metadata or having the client implement the interface?

wcf

What are the pro / cons to having the client consume a WCF service either via adding a reference to the service (and basically everything is generated for you) or having the client implement a shared

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.