dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.05k stars 1.17k forks source link

`new MenuItem() { Header = ... };` cause bind errors #9273

Open Cologler opened 3 months ago

Cologler commented 3 months ago

Description

A empty project (target to .NET 8) with only new MenuItem() { Header = string.Empty }; cause bind errors:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

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

lindexi commented 3 months ago

@Cologler It is designed to work in Menu. And the Menu inherit System.Windows.Controls.ItemsControl.

https://github.com/dotnet/wpf/blob/c9b30d13b2853584d7b3b3dae888261d64b5de6d/src/Microsoft.DotNet.Wpf/src/Themes/XAML/MenuItem.xaml#L834-L839

Cologler commented 3 months ago

@lindexi I want to dynamically generate menu items through programming, rather than through XAML file, but this error is too annoying.

lindexi commented 3 months ago

@Cologler You can define the global style to rewrite this behavior.

pchaurasia14 commented 3 months ago

@Cologler - Can we close this issue then?

Cologler commented 3 months ago

@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.

hongruiyu commented 2 months ago

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?

miloush commented 2 months ago

Is it strange that new MenuItem() { Header = string.Empty } causes binding errors, while new 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.