【问题标题】:Why are submenu items not visible?为什么子菜单项不可见?
【发布时间】:2021-05-11 06:58:24
【问题描述】:

我已将菜单和子菜单绑定到用户定义的类类型列表,其中包含字符串和列表作为其属性。

public class HouseInfo
    {
        public string House
        {
            get;
            set;
        }

        public List<String> Details
        {
            get;
            set;

        }

    }

 public List<HouseInfo> HouseInfos { get; set; }

这是菜单的 xaml:

 <Menu x:Name="menu"
                              VerticalAlignment="Top"
                              Grid.Row="1"
                              Grid.Column="4"
                              Height="19">
                            <MenuItem ItemsSource="{Binding HouseInfos}"
                                      Padding="0"
                                      Background="#0068FF11"
                                      VerticalAlignment="Top"
                                      RenderTransformOrigin="0.5,0.5"
                                      Height="19"
                                      Width="105">
                                <MenuItem.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform />
                                        <SkewTransform />
                                        <RotateTransform />
                                        <TranslateTransform X="0.5" />
                                    </TransformGroup>
                                </MenuItem.RenderTransform>
                                <MenuItem.Header>
                                    <Label x:Name="headerYears"
                                           Margin="0"
                                           Padding="0"
                                           Content="Houses"
                                           Background="#00FF0000"
                                           MaxHeight="18"
                                           UseLayoutRounding="False"
                                           RenderTransformOrigin="0,0"
                                           HorizontalContentAlignment="Center" />
                                </MenuItem.Header>
                                <MenuItem.ItemContainerStyle>
                                    <Style TargetType="{x:Type MenuItem}">
                                        <Setter Property="Header"
                                                Value="{Binding House}" />
                                        <Setter Property="ItemsSource"
                                                Value="{Binding InfoPoints}" />
                                    </Style>
                                </MenuItem.ItemContainerStyle>
                            </MenuItem>
                        </Menu>

数据已正确绑定在子菜单和菜单中,但由于某种未知原因,即使生成的子菜单项的数量等于视图模型中的列表计数,我也无法确定子菜单文本不可见。

如何让它们可见?

我尝试设置填充、对齐、前景和我知道的每个属性,但子菜单项文本仍然不可见。

Invisible Submenu items

【问题讨论】:

  • 我无法重现该问题。所有项目都与您的 XAML 一起显示在我身边。您是否可以为Menu 设置一些隐式样式。

标签: wpf xaml data-binding wpf-controls submenu


【解决方案1】:

我也无法显示子菜单项。花了一点时间,但我找到了一种让它们显示的方法。问题是子菜单项绑定到Details 而不是House 所以Header 没有得到这个。所以我添加了另一个ItemContainerStyle,如下所示:

<MenuItem.ItemContainerStyle>
    <Style TargetType="MenuItem">
        <Setter Property="Header" Value="{Binding Path=House}" />
        <Setter Property="ItemsSource" Value="{Binding Details}" />
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="MenuItem">
                    <Setter Property="Header" Value="{Binding}" />
                </Style>
            </Setter.Value>
        </Setter>
    </Style>
</MenuItem.ItemContainerStyle>

【讨论】:

  • 正是我也想通了...最终我不得不将 infoPoints 也作为类类型并将 infoPoints 保存在 Houses 本身中。
猜你喜欢
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多