Topic Author
Posts: 168
Joined: 08 May 2017, 18:36

Right way to delete codebehind element (Grid) and problem with FindName("grid")

29 Mar 2018, 11:03

I load xaml file which contain other gui elements in to MainWindow grid somewhere. This is working OK.
void noesis_IView_Windows::mf_AddWindow_toGridView(const std::string &pathToXaml)
	Noesis::Ptr<Noesis::FrameworkElement> fe = Noesis::GUI::LoadXaml<Noesis::FrameworkElement>(pathToXaml.c_str());
	Noesis::Grid * g = fe->FindName<Noesis::Grid>("grid_options_view");
If I understand this code give me this result?
<Grid x:Name="grid_sidebar_view" >
	<Grid x:Name="grid_options_view" > <!-- grid_options_view iis loaded from xaml file with all his content -->
	</Grid >
But I have problem find and delete it. This is not working:
Noesis::Grid * grid_sidebar_view = m_NsIViewDelegate->GetContent()->FindName<Noesis::Grid>("grid_sidebar_view");
Noesis::Grid * g = grid_sidebar_view->FindName<Noesis::Grid>(gridName.c_str()); // find nothing g will be empty
if (g != nullptr)
	grid_sidebar_view ->GetChildren()->Remove(g); // never happens because g is empty.
But when I load xaml file in to member object, then I can delete it with this:
Noesis::Ptr<Noesis::FrameworkElement> fe = Noesis::GUI::LoadXaml<Noesis::FrameworkElement>(pathToXaml.c_str());
m_NsGrid_d = fe->FindName<Noesis::Grid>("grid_options_view"); // m_NsGrid_d is member Noesis::Grid * m_NsGrid_d;
// deletion
Noesis::Grid * grid_sidebar_view = m_NsIViewDelegate->GetContent()->FindName<Noesis::Grid>("grid_sidebar_view");
grid_sidebar_view->GetChildren()->Remove(m_NsGrid_d); // only this way I can delete this grid (it dissapear from GUI)
It is right?

Question is, why I can not findName grid created in codebehind?
User avatar
Site Admin
Posts: 3064
Joined: 22 Dec 2011, 19:20

Re: Right way to delete codebehind element (Grid) and problem with FindName("grid")

02 Apr 2018, 12:08

Named elements are only added to root NameScope during xaml loading. That is why initially you can find "grid_options_view" in the root of the just loaded xaml.
But named elements that are added to another UI tree are not automatically registered in the new tree NameScope, but it can be done in code:
m_NsIViewDelegate->GetContent()->RegisterName("grid_options_view", g);
Although this is not the common practice to delete elements. Usually you know the container where dynamic elements are added, and then you directly remove the desired element (knowing its pointer) from there, like you did in the last code block:

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 7 guests