WPF子菜单ItemsPanel

时间:2016-03-30 06:09:33

标签: c# wpf xaml mvvm menu

我想在多个列中显示菜单项..

我想将 WrapPanels 用于菜单

下面的代码适用于第一级,但是当打开子菜单的子菜单或子菜单时,它显示为普通列表。

可以采取哪些措施来覆盖默认行为。

注意:我使用Itemssource来绑定菜单项(我自己的类的对象),所以我不能给出嵌套的项目样式。

<Style TargetType="{x:Type MenuItem}"  BasedOn="{StaticResource {x:Type MenuItem}}">
   ....
   <Setter Property="ItemsSource" Value="{Binding Children}"/>
   ....
   <Setter Property="ItemsPanel">
         <Setter.Value>
             <ItemsPanelTemplate>
                   <WrapPanel MaxHeight="300" Orientation="Vertical"/>
             </ItemsPanelTemplate>
         </Setter.Value>
    </Setter>
     ....
</Style>

1 个答案:

答案 0 :(得分:3)

您可以绑定到任何级别

          <Style TargetType="MenuItem">
                <Setter Property="ItemsSource" Value="{Binding Children}"/>
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <WrapPanel MaxHeight="300" Orientation="Vertical"/>
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="ItemContainerStyle">
                    <Setter.Value>
                            <Style TargetType="MenuItem">
                                <Setter Property="ItemsSource" Value="{Binding Children}"/>
                                <Setter Property="ItemsPanel">
                                    <Setter.Value>
                                        <ItemsPanelTemplate>
                                            <WrapPanel MaxHeight="300" Orientation="Vertical"/>
                                        </ItemsPanelTemplate>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="ItemContainerStyle">
                                    <Setter.Value>
                                        <Style TargetType="MenuItem">
                                            <Setter Property="ItemsSource" Value="{Binding Children}"/>
                                            <Setter Property="ItemsPanel">
                                                <Setter.Value>
                                                    <ItemsPanelTemplate>
                                                        <WrapPanel MaxHeight="300" Orientation="Vertical"/>
                                                    </ItemsPanelTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </Setter.Value>
                </Setter>   
            </Style>