Setting Background Property doesnt cover all space for MenuItem in ContextMenu #18170
-
Describe the bugSetting Background Property doesnt cover all space for MenuItem in ContextMenu I`am trying to set colors for MenuItems but i got this (color dont fill all space) To ReproduceCode
ViewModel: Full project code: https://github.com/SwiftExtender/JsonTable Expected behaviorBackground Color will fill all space Avalonia version11.2.3 OSWindows Additional contextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
It seems that when using |
Beta Was this translation helpful? Give feedback.
-
@nickodei I think it is a bug, i cant somehow bypass this behavior |
Beta Was this translation helpful? Give feedback.
-
I think you shouldn't place a MenuItem in DataTemplate. Instead use ItemContainerTheme to edit Background or other properties. |
Beta Was this translation helpful? Give feedback.
-
@timunie I cant understand how to use ItemContainerTheme for MenuItem. When i try to use ControlTheme with data from view model, ItemContainerTheme for MenuItem is null Code below :
|
Beta Was this translation helpful? Give feedback.
-
Something closer to this:
<ContextMenu ItemsSource="{Binding MacrosContextMenu}">
<ContextMenu.ItemContainerTheme>
<ControlTheme TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}"
x:DataType="vm:MacrosItem">
<Setter Property="Background" Value="{Binding BackgroundColor}"/>
<Setter Property="Foreground" Value="{Binding HeaderTextColor}"/>
<Setter Property="CommandParameter" Value="{Binding ElementName=editor, Path=SelectedText}"/>
</ControlTheme>
</ContextMenu.ItemContainerTheme>
<ContextMenu.ItemTemplate>
<DataTemplate x:DataType="vm:MacrosItem">
<TextBlock Content="{Binding Header}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu> |
Beta Was this translation helpful? Give feedback.
-
@maxkatz6 I`ve tried your advice, now it is working (still have some non-coloured space, but it is acceptable). Big thanks!
|
Beta Was this translation helpful? Give feedback.
Something closer to this:
CommandParameter
to the theme too. But I don't know where "editor" comes from. It might be better done in another way depending on the context.ContextMenu.ItemTemplate
for this simpler scenario can be replaced withContextMenu DisplayMemberBinding="{Binding Header}"
, unless you want to specify more text block properties.