Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid not propagating the changes to its child DataGridTemplateColumn elements #18020

Open
LazaroOnline opened this issue Jan 22, 2025 · 6 comments

Comments

@LazaroOnline
Copy link

LazaroOnline commented Jan 22, 2025

Describe the bug

In a DataGrid, if you add an item to it,
that has a sub list property List<string> MyUrlList {get; set;}
with some urls setted and binded to a
DataGridTemplateColumn > SplitButton > MenuFlyout > ItemsSource,
it loads the row correctly, and the SplitButton.Flyout shows the list of 2 items.

BUT, if you remove the row item from the DataGrid and replace it with another one,
the first row in the DataGrid updates correctly each cell except for
the SplitButton.Flyout that is still pointing
to the list of the first element ever loaded in that position of the DataGrid,
instead of refreshing it.

DataGrid got the update notification and updated the rest of the values,
but DataGrid didn't notify the child SplitButton.Flyout to update it's ItemsSource.

This is a nasty bug because everything looks like it works but then the links and data inside are referencing another person/object!

To Reproduce

AXAML Example:

<DataGrid ItemsSource="{Binding UserList}" AutoGenerateColumns="False">
  <DataGrid.Columns>
    <DataGridTemplateColumn Header="Actions">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate DataType="model:User">
          <SplitButton Content="User Url List" >
            <SplitButton.Flyout>
              <MenuFlyout ItemsSource="{Binding UserLinks}">
                <MenuFlyout.ItemContainerTheme>
                  <ControlTheme TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}" x:DataType="model:Link">
                    <Setter Property="Header" Value="{Binding LinkName}"/>
                  </ControlTheme>
                </MenuFlyout.ItemContainerTheme>
              </MenuFlyout>
            </SplitButton.Flyout>
          </SplitButton>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>

Expected behavior

No response

Avalonia version

11.2.3

OS

Windows

Additional context

No response

@LazaroOnline
Copy link
Author

Another way to put it into words is that the Dynamic MenuFlyout "popup" doesnt get refreshed when it is placed inside a DataGrid's row that has loaded other rows before, then removed the rows and then added new rows.

@timunie
Copy link
Contributor

timunie commented Jan 22, 2025

Does MyUrlList correctly raise propertyChanged?

Please provide a minimum sample to investigate

@LazaroOnline
Copy link
Author

@LazaroOnline
Copy link
Author

LazaroOnline commented Jan 23, 2025

Does MyUrlList correctly raise propertyChanged?

Please provide a minimum sample to investigate

The UrlList can be either List or ObservableCollection but it fails both ways because that property is not the one changing, it is the parent object (the Person object) that contains that list, that is changing, which is part of an ObservableCollection binded to the DataGrid.
It is more clear in the example repository I posted above, let me know if there is any question on how to reproduce it.
For other controls like a ComboBox inside a DataGrid row, it does update the list of elements, it is just the MenuFlyout from the example.
It is not related to the use of a ControlTheme to do the setup of MenuItems, since you can also reproduce it with this simpler example:

<MenuFlyout ItemsSource="{Binding UserLinks}">
  <MenuFlyout.ItemTemplate>
    <DataTemplate DataType="model:Link">
      <MenuItem Header="{Binding LinkName}"/>
    </DataTemplate>
  </MenuFlyout.ItemTemplate>
</MenuFlyout>

I found that simpler way by manually testing, the version using ControlTheme was my first option because in the MenuFlyout docs the dynamic example is that way, (maybe you want to update that to make it simpler).

@LazaroOnline
Copy link
Author

LazaroOnline commented Jan 23, 2025

I was trying alternative methods to setup the MenuItems to have my app working without waiting for a possible fix.
Maybe it can be done using an ItemsRepeater, that way the list gets refreshed correctly (yay!).
But the ItemsRepeater doesn't pass the list of MenuItems to the MenuFlyout without grouping all of them, which results in all the child MenuItems loaded as a group inside a single parent MenuItem (not good).
Here is an example in case someone knows how to fix that child-parent issue:

<MenuFlyout>
  <ItemsRepeater  ItemsSource="{Binding UserLinks}" >
    <ItemsRepeater.ItemTemplate>
      <DataTemplate>
        <MenuItem Header="{Binding LinkName}"/>
      </DataTemplate>
    </ItemsRepeater.ItemTemplate>
  </ItemsRepeater>
</MenuFlyout>

@LazaroOnline
Copy link
Author

I have narrowed it down to particularly the MenuFlyout inside a DataGridRow,
Tryed using <Button.MenuFlyout> instead of SplitButton and got the same issue.
Then tryed to change the MenuFlyout for just a Flyout and it does refresh correctly.

So the bug seems to be in MenuFlyout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants