Open Cologler opened 3 months ago
@Cologler It is designed to work in Menu. And the Menu inherit System.Windows.Controls.ItemsControl
.
@lindexi I want to dynamically generate menu items through programming, rather than through XAML file, but this error is too annoying.
@Cologler You can define the global style to rewrite this behavior.
@Cologler - Can we close this issue then?
@pchaurasia14 Is it strange that new MenuItem() { Header = string.Empty }
causes binding errors, while new MenuItem()
does not? There is no documentation indicating that users should avoid creating MenuItem
objects in code.
lindexi is right, you could set global styles.
In App.xaml, you could set default values for HorizontalContentAlignment and VerticalContentAlignment as follows, I just gave the default value Center
<Application x:Class="Bug001.App"
xmlns="[http://schemas.microsoft.com/winfx/2006/xaml/presentation](https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%2Fpresentation&data=05%7C02%7Cv-hongruiyu%40microsoft.com%7C1cc608c39a38467e5bad08dcabae3564%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638574011927210613%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=G8cNrwR5HulKyOjLBIvqgg%2FxDGhslYSTGfHUSVrdmrg%3D&reserved=0)"
xmlns:x="[http://schemas.microsoft.com/winfx/2006/xaml](https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml&data=05%7C02%7Cv-hongruiyu%40microsoft.com%7C1cc608c39a38467e5bad08dcabae3564%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638574011927225037%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=eJtFX44nv5KO0xhdn7%2FdAtZ37V2x7ZOSwK3aQvoxcsQ%3D&reserved=0)"
xmlns:local="clr-namespace:Bug001"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
</Application.Resources>
</Application>
Do you set global styles to prevent errors?
Is it strange that
new MenuItem() { Header = string.Empty }
causes binding errors, whilenew MenuItem()
does not?
The former has a content, the latter does not. Hence in the former case, it tries to align the content, but doesn't have anything to align it to. The error is output only if tracing is enabled and it can be safely ignored.
There is no documentation indicating that users should avoid creating MenuItem objects in code.
Because that is not the case. You are allowed to create menu items in code.
Description
A empty project (target to .NET 8) with only
new MenuItem() { Header = string.Empty };
cause bind errors:Reproduction Steps
Just call
new MenuItem() { Header = string.Empty };
This is the simplest demo project: https://github.com/Cologler/bug001-wpf
Expected behavior
No binding errors.
Actual behavior
Has binding errors.
Regression?
No response
Known Workarounds
No response
Impact
No response
Configuration
Target to .NET 8
Other information
No response