microsoft / WinUI-Gallery

This app demonstrates the controls available in WinUI and the Fluent Design System.
MIT License
2.65k stars 612 forks source link

Add an example on how to dynamically create the items for a MenuFlyout #1527

Closed ettiennehugo closed 2 months ago

ettiennehugo commented 2 months ago

Issue type

sample request

Which version of the app?

WinUI 3 Gallery

Description

All the samples in the MenuFlyout are statically constructed, please add an example on how to dynamically create these items from some object in a DataTemplate.

Screenshots

No response

Windows version

No response

Additional context

I'm creating a log viewer that can have one or more potential corrections to attached to each log entry, when multiple corrections could apply I want to display a drop-down button with a flyout menu that is connected to Action callbacks to apply corrections/adjustments to the data. Here's an example DataTemplate of what I'm currently trying (does not work in WinUI3):

<DataTemplate x:Key="LogEntryMultiCorrectionTemplate" x:DataType="coreUI:LogEntry">
  <TreeViewItem>
    <StackPanel Orientation="Horizontal" Margin="0" Padding="0">
      <TextBlock Text="{x:Bind Timestamp}" Width="100" Height="24" FontWeight="Bold" Margin="0" Padding="0,2"/>
      <TextBlock Text="{x:Bind Level}" Width="100" Height="24" Foreground="{x:Bind Level, Converter={StaticResource LogLevelToColorConverter}}" FontWeight="Bold" Margin="10, 0" Padding="0,2"/>
      **<DropDownButton Width="48" Height="24" Margin="0" Padding="0" ToolTipService.ToolTip="Select correction to apply">
        <DropDownButton.Flyout>
          <MenuFlyout>
            <MenuFlyoutItemsControl ItemsSource="{x:Bind Corrections, Mode=OneTime}">
              <MenuFlyoutItemsControl.ItemTemplate>
                <DataTemplate x:DataType="coreUI:LogCorrection">
                  <MenuFlyoutItem Text="{x:Bind Name, Mode=OneTime}" Click="MenuFlyoutItem_Click"/>
                </DataTemplate>
              </MenuFlyoutItemsControl.ItemTemplate>
            </MenuFlyoutItemsControl>
          </MenuFlyout>
        </DropDownButton.Flyout>
        <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xEC7A;" Margin="0"/>
      </DropDownButton>**
      <TextBlock Text="{x:Bind Message}" Height="24" TextWrapping="Wrap" Margin="5,0,0,0" Padding="0,2"/>
    </StackPanel>
  </TreeViewItem>
</DataTemplate>
karkarl commented 2 months ago

It is currently not possible to dynamically populate MenuFlyoutItems at runtime. Please refer to this bug on microsoft-ui-xaml repo.

Closing as won't fix.