0

I have listbox with Buttons. Every button have specific name -> button.Name = "button1".

I want to find specific button in listbox by Name.

I tried something like this:

if (listBox.Items.Contains(new Button().Name = "button2"))
{
    MessageBox.Show("TEST");
}

But it doesnt work.

How to find it?

Martin Gabriel
  • 69
  • 1
  • 2
  • 12

2 Answers2

1

You need to check: 1. If the item is a Button 2. If its name is the same (use == not = as in your code)

foreach(var i in listBox.Items)
{    
    if (i is Button && (i as Button).Name=="button2")
    {
        MessageBox.Show("TEST");
    }    
}
Bolu
  • 8,696
  • 4
  • 38
  • 70
0

If you have your ItemsControl item with you then you can iterate its Visualtree to reach to your button using VisualTreeHelper

Recursive find child is explained in this post How can I find WPF controls by name or type?

Community
  • 1
  • 1
Nitin
  • 18,344
  • 2
  • 36
  • 53