WinUI: Creating a DataTemplate through Code doesent resolve all bindings.

I have a binding attachment that lets me add items to a MenuBarItem dynamically. It adds an ItemSource and ItemTemplate.
<MenuFlyoutSubItem Text="{Binding Name}" ctrl:BindableMenuBarItem.ItemsSource="{Binding Operations}" ctrl:PrintAttachment.Print="{Binding Operations}">
<ctrl:BindableMenuBarItem.ItemTemplate>
<DataTemplate>
<MenuFlyoutItem Text="{Binding Name}" CommandParameter="{Binding}" Command="{Binding AddOperationCommand,ElementName=vm}"/>
</DataTemplate>
</ctrl:BindableMenuBarItem.ItemTemplate>
</MenuFlyoutSubItem>
<MenuFlyoutSubItem Text="{Binding Name}" ctrl:BindableMenuBarItem.ItemsSource="{Binding Operations}" ctrl:PrintAttachment.Print="{Binding Operations}">
<ctrl:BindableMenuBarItem.ItemTemplate>
<DataTemplate>
<MenuFlyoutItem Text="{Binding Name}" CommandParameter="{Binding}" Command="{Binding AddOperationCommand,ElementName=vm}"/>
</DataTemplate>
</ctrl:BindableMenuBarItem.ItemTemplate>
</MenuFlyoutSubItem>
The Name and CommandParameter bindings both get set, but the command binding doesent run at all, presumably due to the ElementName. I even created a special print attachment property and if I had ElementName, it didnt print anything, but it did correctly print the other things. This might be because i have to create the DataTemplates in the code behind that it doesent let things with relative bindings get set. This is what I do.
static async void BuildArguments(MenuFlyoutSubItem m, IEnumerable source, DataTemplate template)
{
try
{
Action h = () =>
{
m.Items.Clear();
foreach (var item in source)
{
var content = (MenuFlyoutItemBase)template.LoadContent();
content.DataContext = item;
m.Items.Add(content);
}
};
await m.DispatcherQueue.EnqueueAsync(h, DispatcherQueuePriority.Normal);
}
catch (Exception ex)
{
Debug.WriteLine($"error creating menu bar sub items: '{ex.Message}'");
}
}
static async void BuildArguments(MenuFlyoutSubItem m, IEnumerable source, DataTemplate template)
{
try
{
Action h = () =>
{
m.Items.Clear();
foreach (var item in source)
{
var content = (MenuFlyoutItemBase)template.LoadContent();
content.DataContext = item;
m.Items.Add(content);
}
};
await m.DispatcherQueue.EnqueueAsync(h, DispatcherQueuePriority.Normal);
}
catch (Exception ex)
{
Debug.WriteLine($"error creating menu bar sub items: '{ex.Message}'");
}
}
Is my problem the way I create the child contents, and if so what can I do to fix it.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server