绑定菜单缺少工具提示,并且标题名称周围有几个空格

时间:2018-07-15 16:58:20

标签: wpf binding menu menuitem

所以我有这个菜单Style

<Style TargetType="{x:Type Menu}" x:Key="StandardMenu1">
    <Style.Resources>
        <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="Separator">
            <Setter Property="Height" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Separator">
                        <Border BorderBrush="{StaticResource MenuSeparatorBorderBrush}" BorderThickness="1" Margin="25,0,0,0"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ToolTip">
                <Setter.Value>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Name}" FontSize="12"/>
                        <TextBlock Text="{Binding Id}" FontSize="12"/>
                        <TextBlock Text="{Binding IpAddress}" FontSize="12"/>
                        <TextBlock Text="{Binding MTU}" FontSize="12"/>
                    </StackPanel>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Menu}}}"/>
            <Setter Property="FontSize" Value="{DynamicResource ApplicationFontSize}"/>
            <Setter Property="Command" Value="{Binding Command}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <!--Border 1-->
                        <Border x:Name="Border" Background="Transparent" BorderBrush="Transparent" CornerRadius="2"
                            BorderThickness="1" SnapsToDevicePixels="False">
                            <Grid x:Name="Grid">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" SharedSizeGroup="MenuTextColumnGroup"/>
                                    <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIGTColumnGroup"/>
                                    <ColumnDefinition x:Name="Col3" Width="14"/>
                                </Grid.ColumnDefinitions>
                                <ContentPresenter Grid.Column="0" Margin="{TemplateBinding Padding}" x:Name="HeaderHost" RecognizesAccessKey="True" ContentSource="Header" VerticalAlignment="Center"/>
                                <ContentPresenter Grid.Column="1" Margin="8,1,8,1" x:Name="IGTHost" ContentSource="InputGestureText" VerticalAlignment="Center"/>
                                <Popup IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" 
                                       Placement="Right"
                                       HorizontalOffset="-1" 
                                       x:Name="SubMenuPopup"
                                       Focusable="false"
                                       PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"
                                       AllowsTransparency="True">
                                        <Grid Margin="0,0,0,0">
                                        <!--Border 2-->
                                        <Border x:Name="SubMenuBorder" CornerRadius="5"
                                                BorderBrush="{StaticResource MenuSeparatorBorderBrush}"
                                                BorderThickness="1" 
                                                Background="{StaticResource SubmenuItemBackground}" 
                                                SnapsToDevicePixels="True">
                                            <Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True" Margin="2">
                                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/>
                                            </Grid>
                                            <Border.Effect>
                                                <DropShadowEffect ShadowDepth="2" Color="Black"/>
                                            </Border.Effect>
                                        </Border>
                                        <!--Border 3-->
                                        <Border Margin="1,0,0,0"
                                                x:Name="TransitionBorder"
                                                Width="0" 
                                                Height="2" 
                                                VerticalAlignment="Top"
                                                HorizontalAlignment="Left" 
                                                Background="{StaticResource SubmenuItemBackground}"
                                                SnapsToDevicePixels="False"
                                                BorderThickness="1" 
                                                BorderBrush="{StaticResource SubmenuItemBackground}"/>
                                    </Grid>
                                </Popup>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Role" Value="SubmenuHeader">
                                <Setter Property="DockPanel.Dock" Value="Top"/>
                                <Setter Property="Padding" Value="10,3,0,3"/>
                                <Setter TargetName="Border" Property="MinHeight" Value="22"/>
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource SubmenuItemBackground}"/>
                            </Trigger>
                            <Trigger Property="Role" Value="SubmenuItem">
                                <Setter Property="DockPanel.Dock" Value="Top"/>
                                <Setter Property="Padding" Value="10,3,0,3"/>
                                <Setter TargetName="Border" Property="MinHeight" Value="22"/>
                                <!--<Setter TargetName="ArrowPanel" Property="Visibility" Value="Collapsed"/>-->
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource SubmenuItemBackground}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsHighlighted" Value="true"/>
                                    <Condition Property="Role" Value="TopLevelHeader"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource MenuItemHighlightedBackground}"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsHighlighted" Value="true"/>
                                    <Condition Property="Role" Value="TopLevelItem"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource MenuItemHighlightedBackground}"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsHighlighted" Value="true"/>
                                    <Condition Property="Role" Value="SubmenuHeader"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource SubmenuItemBackgroundHighlighted}"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsHighlighted" Value="true"/>
                                    <Condition Property="Role" Value="SubmenuItem"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource SubmenuItemBackgroundHighlighted}"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSubmenuOpen" Value="true"/>
                                    <Condition Property="Role" Value="TopLevelHeader"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Border" Property="Background" Value="#55B3B3B6"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#55B3B3B6"/>
                                <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0"/>
                            </MultiTrigger>

                            <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                                <Setter TargetName="SubMenuPopup" Property="PopupAnimation" Value="None"/>
                            </Trigger>

                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="False"/>
                                    <Condition Property="IsSubmenuOpen" Value="False"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter Property="Foreground" Value="LightSlateGray"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>

                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                    <Condition Property="IsSubmenuOpen" Value="False"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter Property="Foreground" Value="Silver"/>
                                    <Setter TargetName="Border" Property="Background" Value="#55B3B3B6"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>

                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="False"/>
                                    <Condition Property="IsSubmenuOpen" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter Property="Foreground" Value="Silver"/>
                                    <Setter TargetName="Border" Property="Background" Value="#55B3B3B6"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>

                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                    <Condition Property="IsSubmenuOpen" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter TargetName="Border" Property="Background" Value="#55B3B3B6"/>
                                    <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
                                    <Setter Property="Foreground" Value="Silver"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>

                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsVisible}" Value="False"/>
                                </MultiDataTrigger.Conditions>
                                <Setter Property="IsEnabled" Value="False"/>
                                <Setter Property="Foreground" Value="{StaticResource MenuDisabledForeground}"/>
                                <Setter Property="ToolTip" Value="Management interface"/>
                            </MultiDataTrigger>

                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding ShowToolTip}" Value="False"/>
                                </MultiDataTrigger.Conditions>
                                <Setter Property="ToolTip" Value="Machine interfaces"/>
                            </MultiDataTrigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
    <Setter Property="Background" Value="{StaticResource LightBackground}"/>
    <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
</Style>

查看模型:

public class MenuItemViewModel : INotifyPropertyChanged
{
    private readonly ICommand _command;
    public event PropertyChangedEventHandler PropertyChanged;

    public string Name { get; set; }
    public string Description { get; set; }
    public string Id { get; set; }
    public bool IsManagement { get; set; }
    public string IpAddress { get; set; }
    public string MTU { get; set; }
    public bool ShowToolTip { get; set; }
    public int Width { get; set; }
    public bool _isSelected { get; set; }
    public bool _isVisible { get; set; }
}

我的Menu

<Menu Name="menuInterfaces" ItemsSource="{Binding MenuItems}" Margin="0,6,0,0" Style="{StaticResource StandardMenu1}">
    <Menu.ItemTemplate>
        <HierarchicalDataTemplate DataType="{x:Type Menu:MenuItemViewModel}" ItemsSource="{Binding Path=MenuItems}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="12"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="pack://application:,,,/Resources/checked_lightslategray.ico"
                       Width="12"
                       Height="12"
                       Grid.Column="0"
                       Margin="0,0,0,0">
                    <Image.Style>
                        <Style TargetType="Image">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
                                    <Setter Property="Visibility" Value="Visible"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=IsSelected}" Value="False">
                                    <Setter Property="Visibility" Value="Hidden"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
                <TextBlock Text="{Binding Description}"
                           Grid.Column="1"
                           Margin="8,0,0,0"/>
            </Grid>
        </HierarchicalDataTemplate>
    </Menu.ItemTemplate>
</Menu>

创建菜单

对,我有这个bindint ObservableCollection

ObservableCollection<MyData> applicationViewModel.Interfaces;

然后从这个集合中创建我的MenuItemViewModel对象:

menuItemViewModelCollection = new ObservableCollection<MenuItemViewModel>();
foreach (NetworkInterface item in applicationViewModel.Interfaces)
{
    menuItemViewModelCollection.Add(new MenuItemViewModel
    {
        Description = item.Description,
        Id = item.Id,
        IpAddress = item.IpAddress,
        IsManagement = item.IsManagement,
        IsSelected = item.IsSelected,
        Name = item.Name,
        IsVisible = item.IsVisible,

    });
}

ApplicationViewModel.MenuItems = new ObservableCollection<MenuItemViewModel>
    {
        new MenuItemViewModel { Description="  Interfaces", IsVisible = true, ShowToolTip = false,
            MenuItems = menuItemViewModelCollection
        },
    };

结果

enter image description here

所以我有2个问题:

  1. 您可以看到,而不是看到标题Interfaces,而是看到周围有几个空格(也许我不需要创建此接口MenuItemViewModel吗?)

  2. 我只能为此ToolTip看到我的Interfaces,而看不到其中的所有元素。

0 个答案:

没有答案