0

Is there any way to take <someControl> name to a string format variable programically?

For example:

<Canvas Name="zem">
    <Image Name="pav_kelias" />
</Canvas>

C#:

Image pav = new Image();
pav = (Image)zem.Children[0];

Now I linked my <Image> and I want to get it's name - "pav_kelias".

Am I right ? :

string name = pav.Name;

When I try this my program crash.

armandasalmd
  • 169
  • 3
  • 10
  • Any exception message? – Kalten Mar 20 '16 at 14:14
  • I setup a small project and call your c# code in a `Loaded` event of the window and this is working as expected. Where do you call `pav.Name` ? – Kalten Mar 20 '16 at 14:21
  • One of the most frequently accessed WPF questions concerns finding controls by name and type - reference it for help: http://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type – Taterhead Mar 20 '16 at 14:48
  • 2
    @armandasalmd it depends when you do it. If before `InitializeComponents()` is called then it will crash, if after then it should be fine – dkozl Mar 20 '16 at 15:43
  • 1
    And note that `Image pav = new Image();` is useless. Just write `Image pav = (Image)zem.Children[0];` or `var pav = (Image)zem.Children[0];`. – Clemens Mar 20 '16 at 16:06

0 Answers0