sacc1298
Topic Author
Posts: 5
Joined: 08 Sep 2022, 08:15

Issues setting a data context for parent Grid

08 Sep 2022, 18:38

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:
<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>
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:
element->Initialized() += Noesis::MakeDelegate(inventory.GetPtr(), &InventoryUI::OnXamlInitialized);
Thanks!
 
sacc1298
Topic Author
Posts: 5
Joined: 08 Sep 2022, 08:15

Re: Issues setting a data context for parent Grid

08 Sep 2022, 19:27

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
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Issues setting a data context for parent Grid

08 Sep 2022, 19:36

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
element->SetDataContext(MakePtr<ViewModel>());
 
sacc1298
Topic Author
Posts: 5
Joined: 08 Sep 2022, 08:15

Re: Issues setting a data context for parent Grid

08 Sep 2022, 19:50

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?
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Issues setting a data context for parent Grid

08 Sep 2022, 19:55

Same as our examples. Could you please paste code showing how you are setting it?
 
sacc1298
Topic Author
Posts: 5
Joined: 08 Sep 2022, 08:15

Re: Issues setting a data context for parent Grid

08 Sep 2022, 20:01

First I load the XAML I posted above like this:
Noesis::Ptr<Noesis::FrameworkElement> element = Noesis::GUI::LoadXaml<Noesis::FrameworkElement>(xamlName);
Then I immediately set the data context like below:
element->SetDataContext(Noesis::MakePtr<InventoryUI>());
but this gives me an error "Can't set value, DependencyProperty is null" (referring to DataContextProperty).
 
sacc1298
Topic Author
Posts: 5
Joined: 08 Sep 2022, 08:15

Re: Issues setting a data context for parent Grid

08 Sep 2022, 23:04

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!
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Issues setting a data context for parent Grid

09 Sep 2022, 10:58

Thanks for the update, as you said it was very strange what you were experiencing.
Marking this as solved then.

Who is online

Users browsing this forum: No registered users and 7 guests