none
MenuItem 绑定集合属性,无论绑定属性写什么,都显示实体toString结果,但改回原生的style就没问题,我怀疑是style有问题 RRS feed

  • 问题

  •    <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Icon" Value="Nut" ></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
    
                        <Grid ToolTip="{TemplateBinding Header}" Height="24"   Background="black" Width="{TemplateBinding Width}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="30" />
                                <ColumnDefinition Width="*" MinWidth="80"/>
                            </Grid.ColumnDefinitions>
                            <Border Background="black"/>
                            <Border x:Name="_border"   Grid.ColumnSpan="2"/>
                            <materialDesign:PackIcon x:Name="_imgicon" Foreground="white"  Margin="5,3,0,0"  Kind="{Binding Icon,RelativeSource={RelativeSource TemplatedParent}}" >
                            </materialDesign:PackIcon>
                            <TextBlock Foreground="{DynamicResource PanelFontColor}" Grid.Column="1" Margin="5 0 15 0"  VerticalAlignment="Center" x:Name="_txt" Text="{Binding Header,RelativeSource={RelativeSource TemplatedParent}}" />
                            <Path  Visibility="Collapsed" x:Name="_path" Grid.Column="1" HorizontalAlignment="Right"  Margin="5 13 5 0"   Data="M0,4 4,0 0,-4 z" Fill="white"/>
                            <Popup  Placement="Right" x:Name="SubMenuPopup" AllowsTransparency="True"   Focusable="False" IsOpen="{TemplateBinding IsSubmenuOpen}" >
                                <ScrollViewer   ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                                    <Border  Name="SubmenuBorder"   BorderBrush="black" BorderThickness="1"  SnapsToDevicePixels="True"    >
                                        <StackPanel   IsItemsHost="True"   KeyboardNavigation.DirectionalNavigation="Cycle" />
                                    </Border>
                                </ScrollViewer>
                            </Popup>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="HasItems" Value="true">
                                <Setter TargetName="_path" Property="Visibility" Value="Visible"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="_border" Property="Background" Value="green"/>
                                <Setter TargetName="_txt" Property="Foreground" Value="red"/>
                                <Setter TargetName="_path" Property="Fill" Value="blue"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    使用MenuItem设置Itemsource,设置ItemTemplate以及内部的DataTemplate,显示的内容是绑定实体的toString结果,不是DataTemplate显示的模板,我怀疑是不是我的style设置有问题,然后我把MenuItem的style设置成wpf原生的Menuitem的style,DataTemplate就显示了。我自定义的MenuItem的style有什么问题吗?




    • 已编辑 Trian555 2019年9月29日 1:42
    2019年9月29日 1:42

答案

  • Hi    Trian555,

    >>使用MenuItem设置Itemsource,设置ItemTemplate以及内部的DataTemplate,显示的内容是绑定实体的toString结果,不是DataTemplate显示的模板,我怀疑是不是我的style设置有问题,然后我把MenuItem的style设置成wpf原生的Menuitem的style,DataTemplate就显示了。我自定义的MenuItem的style有什么问题吗

    你需要定义ItemContainerStyle这一属性,该属性用来给ItemsControl中包含的每一个Item的容器定义样式。

         <MenuItem.ItemContainerStyle>
                            <Style>
                                <Setter Property="MenuItem.Header" Value="{Binding Title}"/>
                            </Style>
                        </MenuItem.ItemContainerStyle>
    

    Or:

           <Setter Property="ItemContainerStyle">
                    <Setter.Value>
                        <Style>
                            <Setter Property="MenuItem.Header" Value="{Binding Title}"/>
                        </Style>
                    </Setter.Value>
                </Setter>
    Best regards

    Yong Lu

     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Trian555 2019年9月30日 8:03
    2019年9月30日 6:44
    版主