<...">

"FindName" does not work if the item is added to the code

In a WPF application, if ContentControl is declared in XAML,

<Grid Name="MyGrid">
    <ContentControl Name="MyContentControl" />
</Grid>

then I can easily reference it in code with FindName:

ContentControl cc = FindName("MyContentControl") as ContentControl;
cc.Content = ...

But if you add ContentControl code instead:

 ContentControl contentcntr = new ContentControl();
 contentcntr.Name = "MyContentControl";
 this.MyGrid.Children.Add(contentcntr);

FindName doesn't find him.

What is wrong with him in the second case? Who cares?

+3
source share
1 answer

The XAML parser automatically registers the names in the name browser, if you create such elements, you may need to do this yourself, using RegisterName, (There is an accessor onFrameworkElement .)

+8
source

All Articles