Issues setting a data context for parent Grid
Posted: 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:
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);