0

I'm trying to get an XAML element with its own name from another C# class, but it's returning always null, why?

Here's the target XAML with its C# class:

XAML:
<Page
    x:Class="myApp.SubSpace.SomePage"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:myApp.SubSpace"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="target">
    </Grid>
</Page>

C#:
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace myApp.SubSpace 
{
    public sealed partial class SomePage : Page
    {
        public SomePage()
        {
            InitializeComponent();
        }
    }
}

And here's the code it's giving me a null result:

using myApp.SubSpace;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace myApp.Core
{
    public class Main
    {
        private Grid target;
        public Main() {
            FrameworkElement page = new SomePage();
            this.target = (Grid)page.FindName("target");
        }
    }
}

The code is very simplified, but the very important things are there. After calling a new Main object, its target field is null. What's wrong?

Thanks in advance!

nvoigt
  • 75,013
  • 26
  • 93
  • 142
JordiVilaplana
  • 485
  • 3
  • 9
  • What do you want that for? – Federico Berasategui Mar 13 '14 at 15:59
  • @HighCore As I said, the code is very simplified, but after that the `target` object must be edited. – JordiVilaplana Mar 13 '14 at 16:01
  • possible duplicate of [FindName returning null](http://stackoverflow.com/questions/2214737/findname-returning-null) – nvoigt Mar 13 '14 at 16:01
  • where you are assigning your XAML to your Class SomePage() ?? I can't see any code in SomePage which involves XAML. – Aftab Ahmed Mar 13 '14 at 16:01
  • object must be "edited" - that's what [**DataBinding**](http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx) is for. **It is strongly recommended that you use proper DataBinding in XAML-based technologies as opposed to a traditional, procedural code approach. This is because the Visual Tree is a complex structure and dealing with it becomes too cumbersome.** – Federico Berasategui Mar 13 '14 at 16:02
  • @nvoigt I already visited that post, but it solved nothing to me. In that post the element was generated by code, here I'm trying to retrieve the already defined element from another class in another namespace. – JordiVilaplana Mar 13 '14 at 16:08
  • @HighCore Can't bind the data because i need to edit it by hard coding. I need the element to be very flexible. – JordiVilaplana Mar 13 '14 at 16:12
  • @JordiVilaplana you can do as much flexibility as you need, see [this example](http://stackoverflow.com/a/15821573/643085) of a dynamic UI done in proper DataBinding. – Federico Berasategui Mar 13 '14 at 16:14
  • XAML's idea of 'dynamic' is very different from other technologies. You need to learn to use `DataTemplates` and other XAML-specific constructs in order to do this. Don't try to force a procedural approach into XAML. It's just not going to work. – Federico Berasategui Mar 13 '14 at 16:15

3 Answers3

1

I have a helper static class based on the code snippet in Tips and Tricks for C# Metro developer: How to find a XAML control by its name. It hasn't let me down yet.

Simon Munro
  • 5,399
  • 6
  • 33
  • 40
0
Grid myGrid= 
this.FindName("target") as Grid;
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

I realize this wasn't the case in the OP's code but I had the same error (null returned from FindChild) and for me it was an issue of calling FindChild in the constructor of a Page before InitializeComponent() was called. Make sure everything is initialized and ready before looking at the children.

ickydime
  • 1,051
  • 10
  • 22