Skip to main content

Microsoft Silverlight

Answered Question How to find control by type??RSS Feed

(0)

addanki
addanki

Member

Member

16 points

46 Posts

How to find control by type??

It is possible to find the control by name, as below.

UserControl ctrl = (UserControl)System.Windows.Markup.XamlReader.Load(str); // str contains some usercontrol xaml

Button button = ctrl.FindName("ID") as Button;

My problem is, is it possible to find the control by type??

Button btn = ctrl.FindControl("Button") as Button;

if more than one button control is there, then it should return the ButtonList or it should be possible to set the starting index to search for the control.

Please let me know the approach to do in the above manner?

ksleung
ksleung

Contributor

Contributor

5366 points

1,028 Posts

Answered Question

Re: How to find control by type??

Don't know if there is a shorter answer but you can use VisualTreeHelper to (recursively) traverse the descendents of the user control, and test whether a descendent control is a Button...

if (control is Button) { do_something(control); }

addanki
addanki

Member

Member

16 points

46 Posts

Re: How to find control by type??

Hi,

Thanks for your quick reply.

I am able retrieve the descendents, but, after adding to the page, only button is visible, but the rest of the controls like TextBlock, TextBox which are there inside the XAML string are not visible?

Any idea what might be the mistake??

 

addanki
addanki

Member

Member

16 points

46 Posts

Re: How to find control by type??

I got my mistake..

Thanks

Ashok

  • Unanswered Question
  • Answered Question
  • Announcement