TimberStalker
TimberStalker
CC#
Created by TimberStalker on 6/4/2024 in #help
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.
1 replies
CC#
Created by TimberStalker on 5/3/2023 in #help
❔ Fire and forget
Is there a recommended way to call an async void, or to run an async task as fire and forget? I havent been able to find any that doesent cause a warning, and i would rather not supress it.
10 replies