In the following avalonia DataGrid axaml example, the DataGrid works fine except for the SplitButton's MenuFlyout ItemsSource that gets stuck to the first list it loads even if I clear the DataGrid and fill it again. It keeps showing the list of the first element loaded at that position in the DataGrid.
Is there any way to notify MenuFlyout ItemsSource
that it should be updated/refreshed?
Here is a simple repo with a working example: https://github.com/LazaroOnline/AvaloniaDataGridRowFlyoutRefresh-BugSample
Even if this could be a bug in Avalonia (or maybe not), my question is how to use a different binding that works around this and makes it work refreshing the menu items.
Notice that the problem is not about using
ObservableCollection
, since it is not adding/removing items to the collection, but the whole parent of the collection, which is the row's DataContext, has changed.
Using Avalonia v11.2.3
<DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="model:MyModel">
<SplitButton Content="My Url List" Command="{Binding OpenMyMainUrl}"
<SplitButton.Flyout>
<MenuFlyout ItemsSource="{Binding MyUrlList}">
<MenuFlyout.ItemContainerTheme>
<ControlTheme TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}" x:DataType="model:Link">
<Setter Property="Header" Value="{Binding MyText}"/>
<Setter Property="Command" Value="{Binding OpenUrl}"/>
</ControlTheme>
</MenuFlyout.ItemContainerTheme>
</MenuFlyout>
</SplitButton.Flyout>
</SplitButton>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
It does seem to be an issue with MenuFlyout
(posted on GitHub here #18020) at least as of Avalonia v11.2.3.
I found a work-around by using a Flyout
instead of MenuFlyout
, the only catch is having to add the styles so it looks like a "menu", but it does refresh the elements.
Here is the work-around:
<DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="model:MyModel">
<SplitButton Content="My Url List" Command="{Binding OpenMyMainUrl}"
<SplitButton.Flyout>
<Flyout>
<Grid Margin="-12,-8">
<ItemsControl ItemsSource="{Binding MyUrlList}">
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="model:Link">
<MenuItem Header="{Binding MyText}" Command="{Binding OpenUrl}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Flyout>
</SplitButton.Flyout>
</SplitButton>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>