Right way to delete codebehind element (Grid) and problem with FindName("grid")
I load xaml file which contain other gui elements in to MainWindow grid somewhere. This is working OK.
If I understand this code give me this result?
But I have problem find and delete it. This is not working:
But when I load xaml file in to member object, then I can delete it with this:
It is right?
Question is, why I can not findName grid created in codebehind?
Code: Select all
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");
m_NsIViewDelegate->GetContent()->FindName<Noesis::Grid>("grid_sidebar_view")->GetChildren()->Add(g);
}
Code: Select all
<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 >
</Grid>
Code: Select all
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.
}
Code: Select all
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)
Question is, why I can not findName grid created in codebehind?
-
-
sfernandez
Site Admin
- Posts: 2027
- Joined:
Re: Right way to delete codebehind element (Grid) and problem with FindName("grid")
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:
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:
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:
Code: Select all
m_NsIViewDelegate->GetContent()->RegisterName("grid_options_view", g);
Code: Select all
m_sidebarView->GetChildren()->Remove(m_selectedChild);
Who is online
Users browsing this forum: No registered users and 4 guests