Issues setting a data context for parent Grid
I have a XAML which looks like this and is meant to be an (incomplete) simple inventory slot UI using a Grid as a root parent element:
I would like to set a data context in C++ code for the Grid so all child elements inherit that data context, and I have a question regarding that:
1) I do not use the application framework at all so everything I am doing is manual like the Integration sample. After calling LoadXAML, a FrameworkElement is returned (I assume this is the root parent Grid) and I add a delegate to the Initialized() event handler immediately after loading (this delegate sets the data context) but the Initialized event is never fired for this root element so the delegate is never called. I might be doing something wrong on my end on the XAML side so any advice/thoughts would be great.
This is how I add the delegate immediately after LoadXAML:
Thanks!
Code: Select all
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
Width ="600"
Height ="300">
<ItemsControl x:Name="InventorySlots" ItemsSource="{Binding Inventory}" Focusable="False">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="9" Margin="5"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}" Style="{StaticResource InventorySlotStyle}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<system:String>String Test</system:String>
</ItemsControl>
</Grid>
1) I do not use the application framework at all so everything I am doing is manual like the Integration sample. After calling LoadXAML, a FrameworkElement is returned (I assume this is the root parent Grid) and I add a delegate to the Initialized() event handler immediately after loading (this delegate sets the data context) but the Initialized event is never fired for this root element so the delegate is never called. I might be doing something wrong on my end on the XAML side so any advice/thoughts would be great.
This is how I add the delegate immediately after LoadXAML:
Code: Select all
element->Initialized() += Noesis::MakeDelegate(inventory.GetPtr(), &InventoryUI::OnXamlInitialized);
Re: Issues setting a data context for parent Grid
More information on this: Adding the delegate does in fact add an entry for "Initialized" in mEventHandlers but somehow by the time the Initialized event is fired this entry is removed and replaced with a Converter<Single> entry for some reason
Re: Issues setting a data context for parent Grid
After calling LoadXAML you receive an already initialized tree. Initialized even will never called at your point, it is too late. That's only useful for code-behind.
But in your case, you can just set the DataContext directly. Something like
But in your case, you can just set the DataContext directly. Something like
Code: Select all
element->SetDataContext(MakePtr<ViewModel>());
Re: Issues setting a data context for parent Grid
I tried doing it manually like that after loading but the DataContextProperty was nullptr so it fails to set the data context. Am I missing something in the XAML for DataContextProperty to be nullptr?
Re: Issues setting a data context for parent Grid
Same as our examples. Could you please paste code showing how you are setting it?
Re: Issues setting a data context for parent Grid
First I load the XAML I posted above like this:
Then I immediately set the data context like below:
but this gives me an error "Can't set value, DependencyProperty is null" (referring to DataContextProperty).
Code: Select all
Noesis::Ptr<Noesis::FrameworkElement> element = Noesis::GUI::LoadXaml<Noesis::FrameworkElement>(xamlName);Code: Select all
element->SetDataContext(Noesis::MakePtr<InventoryUI>());Re: Issues setting a data context for parent Grid
False alarm. It was some messy DLL related stuff and how I was linking Noesis. I was using Noesis.lib in more than one DLL (a requirement) so there were 2 copies of global variables and they were being initialized in one DLL but were all NULL in the other DLL. I now use the Noesis.dll and it works as its all shared among the other DLLs. It definitely did not make sense that I could not set a data context for a simple XAML like above. Thanks for the help anyway!
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Issues setting a data context for parent Grid
Thanks for the update, as you said it was very strange what you were experiencing.
Marking this as solved then.
Marking this as solved then.
Who is online
Users browsing this forum: No registered users and 7 guests