Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

MenuFlyout ItemContainerTheme Binding Bug #18146

Closed
sachssoft opened this issue Feb 7, 2025 · 3 comments
Closed

MenuFlyout ItemContainerTheme Binding Bug #18146

sachssoft opened this issue Feb 7, 2025 · 3 comments

Comments

@sachssoft
Copy link

sachssoft commented Feb 7, 2025

Describe the bug

I'm building a customizable toolbar like MS Word Toolbar. My concept is a lot of XAML and less code. It actually worked. Some controls like MenuFlyout, which I have known for a long time, always have issues.

The type of the property "Items" is IEnumerable of TrayItem. It works and contains two items. (See image below)
But the binding is wrong. I looked with a debugger and found the issue. The DataContext of each item is MainWindowsViewModel instead TrayItem. Why is it there? Actually it's not true!!

<MenuFlyout ItemsSource="{Binding Items, RelativeSource={...
Image

I could solve the problem with just code, but I really want XAML. Unfortunately it doesn't work.

...
<ControlTemplate>
	<DropDownButton Classes="Tool"
					Padding="0"
					Margin="0"
					x:Name="PART_DropDownButton">
		<DropDownButton.Flyout>
			<MenuFlyout ItemsSource="{Binding Items, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:TrayItem}}}">
				<MenuFlyout.ItemContainerTheme>
					<ControlTheme TargetType="MenuItem"
								  BasedOn="{StaticResource {x:Type MenuItem}}"
								  x:DataType="controls:TrayItem">
						<Setter Property="Icon">
							<Template>
								<PathIcon Data="{Binding EffectiveIconData}" /> <!--DOES NOT WORK!!--> 
							</Template>
						</Setter>
						<Setter Property="Header" Value="{Binding EffectiveLabel}"/> <!--DOES NOT WORK!!--> 
					</ControlTheme>
				</MenuFlyout.ItemContainerTheme>
			</MenuFlyout>
		</DropDownButton.Flyout>
		...
	</DropDownButton>
</ControlTemplate>
...

See screenhot:
Image

I only used the test converter for debugging and removed "EffectiveLabel" to look at it.
<Setter Property="Header" Value="{Binding Converter={StaticResource TestConverter}}"/>

It doesn't work with "RelativeSource" either.

<Setter Property="Header" Value="{Binding Converter={StaticResource TestConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:TrayItem}}}"/> <!--DOES NOT WORK!!--> 

Image

It doesn't work with "RelativeSource" either.

<Setter Property="Header" Value="{Binding Converter={StaticResource TestConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:TrayItem}}}"/> <!--DOES NOT WORK!!--> 

However, when I add $self, the result shows MenuItem with the wrong DataContext MainWindowsViewModel. The DataContext should actually be TrayItem.

To Reproduce

I'm building my own toolbar. The name Tray is unrelated to SystemTray or ToolbarTray.

1 Create a customizable toolbar
2 Add many items (TrayItem)
3 Add two underlying items in a item (TrayItem)

This item, TrayItem, automatically contains a DropDownButton because it holds two items and does not have a command source.

VisualTree shows this structure

Image

Expected behavior

No response

Avalonia version

11.2.1

OS

Windows

Additional context

No response

@sachssoft sachssoft added the bug label Feb 7, 2025
@timunie
Copy link
Contributor

timunie commented Feb 7, 2025

Try NamedControls binding. Relative source uses visual tree and your Popup isn't part of that tree.

@sachssoft
Copy link
Author

What do you mean NamedControls binding?

I forgot to say. It only works with DataTemplate. But this element is in the content of MenuItem. Unfortunately, I find it too ugly.

<MenuFlyout.ItemTemplate>
	<DataTemplate>
		<Button Background="{x:Null}"
				BorderThickness="0"
				Command="{Binding Source.Command}"
				CommandParameter="{Binding Source.CommandParameter}">
			<StackPanel Orientation="Horizontal">
				<Viewbox Stretch="Uniform">
					<PathIcon Data="{Binding EffectiveIconData}" />
				</Viewbox>
				<Label Content="{Binding Label}"/>
			</StackPanel>
		</Button>
	</DataTemplate>
</MenuFlyout.ItemTemplate>

Unfortunately, the {Binding} does not work in ItemContainerTheme! DataContext of each MenuItem is wrong. The value TrayItem should be set in DataContext. It that correct?

@timunie
Copy link
Contributor

timunie commented Feb 10, 2025

I can't reproduce it. Please note that x:DataType is your Items Type, not the parent control or anything else.

If the issue persists, please file a minimal sample to reproduce.

@timunie timunie removed the bug label Feb 10, 2025
@AvaloniaUI AvaloniaUI locked and limited conversation to collaborators Feb 10, 2025
@timunie timunie converted this issue into discussion #18162 Feb 10, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants