3 Replies - 379 Views - Last Post: 13 September 2017 - 06:45 AM Rate Topic: -----

#1 jurackavlasta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-September 17

WPF get children from layout by name

Posted 12 September 2017 - 05:54 PM

Hi guys i have really special problem.... I need return child from layout by name... But method findName works only on XAML. But i create layout with label programmatically and i neeed search Label by name from layout. Thanks for some help.
Grid g = (Grid) tasks.SelectedItem;
Label b = (Label)g.FindName("idLabel");
Console.WriteLine(b.Content); //b is null


And guys i havent variable i know only name..

Is This A Good Question/Topic? 0
  • +

Replies To: WPF get children from layout by name

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5823
  • View blog
  • Posts: 19,820
  • Joined: 05-May 12

Re: WPF get children from layout by name

Posted 12 September 2017 - 07:02 PM

View Postjurackavlasta, on 12 September 2017 - 08:54 PM, said:

But i create layout with label programmatically

If you created it programmatically, why not simply hold on to that reference when you created it and pass it where you need it later?

In general though, if you need to search for a control by name or if you need a variable referencing a control you are using WPF incorrectly.
Was This Post Helpful? 0
  • +
  • -

#3 jurackavlasta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-September 17

Re: WPF get children from layout by name

Posted 13 September 2017 - 02:28 AM

thanks for response. You first idea is good but no so effective. And you second comment i used findName() right but method function only with xaml and i create layout programmatically.

This post has been edited by Skydiver: 13 September 2017 - 08:53 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 362
  • Joined: 01-February 16

Re: WPF get children from layout by name

Posted 13 September 2017 - 06:45 AM

As SkyDiver said, if you need to reference the name you are not using WPF correctly. If you have set up the binding correctly you can use the binding property to acquire any information that you need.

One last point, why do you have Console.WriteLine in a WPF application? Why not use Debug.WriteLine
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1