简体   繁体   English

WPF用户控件中的错误

[英]Bug in WPF user control

I created a user control with a menu in WPF. 我在WPF中使用菜单创建了一个用户控件。 It was working just fine and now I'm seeing a format issue with the MenuItem.Header . 一切正常,现在我看到MenuItem.Header出现格式问题。 Basically if I add a header where the name has a "_" character it deletes it. 基本上,如果我在标题中添加一个带有“ _”字符的标题,则会将其删除。 For instance, if I add this to the header: "Test_Header_Name," it shows as: "TestHeader_Name." 例如,如果我将其添加到标题“ Test_Header_Name”,则显示为:“ TestHeader_Name”。 So basically it formats/manipulates the string and deletes the first "_" character. 因此,基本上,它格式化/处理字符串并删除第一个“ _”字符。 I know things don't just happened out of the blue. 我知道事情并不会突然发生。 I am sure, I must have done something at some point to change the format... but I cannot figure out what it was. 我敢肯定,我必须在某些时候做一些事情来更改格式...但是我无法弄清楚它是什么。

Here is the C# to test the menu: 这是C#测试菜单:

 public partial class MenuControl : UserControl
{
    public MenuControl()
    {
        InitializeComponent();
        MenuItem main = new MenuItem();
        main.Header = "Sum(Test_Header_Name)";
        for (int i = 0; i < 10; i++)
        {
            main.Items.Add("Test_" + i);
        }
        main.Items.Add(new Separator());
        main.Items.Add("Remove");
        this.PrincipalMenu.Items.Add(main);
    }
}

And this is the XAML (which is where I think it is happening): 这就是XAML(我认为它正在发生):

 <UserControl x:Class="WindowsFormsApplication1.MenuControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="41" d:DesignWidth="380">
<UserControl.Resources>
    <!--**************************
* STYLE: MenuItem
************************** -->
    <SolidColorBrush x:Key="HighlightedBackgroundBrush" Color="#FFDEDEDE" />
    <SolidColorBrush x:Key="MenuBackgroundBrush" Color="White" />
    <SolidColorBrush x:Key="NormalBorderBrush" Color="#FFE5DFDF" />
    <SolidColorBrush x:Key="SolidMenuFontBrush" Color="Black" />
    <SolidColorBrush x:Key="HighlightedText" Color="White" />
    <SolidColorBrush x:Key="menuItemBrush" Color="#FFB7B7B7" />
    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
    <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
    <Style x:Key="{x:Type Menu}" TargetType="{x:Type Menu}">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Height" Value="25"/>
        <Setter Property="Foreground" Value="{StaticResource SolidMenuFontBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Menu}">
                    <Border
        Background="{StaticResource MenuBackgroundBrush}"
        BorderBrush="{StaticResource MenuBackgroundBrush}"
        BorderThickness="1">
                        <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
                    </Border>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MenuItem}">
                    <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition x:Name="Col0" MinWidth="17" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup"/>
                                <ColumnDefinition Width="Auto" SharedSizeGroup="MenuTextColumnGroup"/>
                                <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIGTColumnGroup"/>
                                <ColumnDefinition x:Name="Col3" Width="14"/>
                            </Grid.ColumnDefinitions>

                            <!-- ContentPresenter to show an Icon if needed -->
                            <ContentPresenter Grid.Column="0" Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/>

                            <!-- Glyph is a checkmark if needed for a checkable menu -->
                            <Grid Grid.Column="0" Visibility="Hidden" Margin="4,0,6,0" x:Name="GlyphPanel" VerticalAlignment="Center">
                                <Path x:Name="GlyphPanelpath" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" FlowDirection="LeftToRight"/>
                            </Grid>

                            <!-- Content for the menu text etc -->
                            <ContentPresenter Grid.Column="1" Margin="{TemplateBinding Padding}" x:Name="HeaderHost" RecognizesAccessKey="True" ContentSource="Header"/>

                            <!-- Content for the menu IGT -->
                            <ContentPresenter Grid.Column="2" Margin="8,1,8,1" x:Name="IGTHost" ContentSource="InputGestureText" VerticalAlignment="Center"/>

                            <!-- Arrow drawn path which points to the next level of the menu -->
                            <Grid Grid.Column="3" Margin="4,0,6,0" x:Name="ArrowPanel" VerticalAlignment="Center">
                                <Path x:Name="ArrowPanelPath" HorizontalAlignment="Right" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,0 L0,8 L4,4 z"/>
                            </Grid>
                            <!--IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}"
                            PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"
                            -->
                            <!-- The Popup is the body of the menu which expands down or across depending on the level of the item -->
                            <Popup  Placement="Right" IsOpen="{TemplateBinding IsSubmenuOpen}" x:Name="SubMenuPopup"  Focusable="false" PopupAnimation="Fade">
                                <Border x:Name="SubMenuBorder" SnapsToDevicePixels="True" Background="{StaticResource WindowBackgroundBrush}"  BorderBrush="{StaticResource SolidBorderBrush}" BorderThickness="1" Padding="2,2,2,2">
                                    <Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True">
                                        <!--StackPanel holds children of the menu. This is set by IsItemsHost=True-->
                                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/>
                                        <!--<ScrollViewer CanContentScroll="True">
                                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                                        </ScrollViewer>-->
                                    </Grid>
                                </Border>
                            </Popup>
                        </Grid>
                    </Border>

                    <!-- These triggers re-configure the four arrangements of MenuItem to show different levels of menu via Role -->
                    <ControlTemplate.Triggers>
                        <!-- Role = TopLevelHeader : this is the root menu item in a menu; the Popup expands down                             
                        2/6/2013 Luis Garcia
                        -->
                        <!--<Trigger Property="Role" Value="TopLevelHeader">
                            <Setter Property="Padding" Value="6,1,6,1"/>
                            <Setter Property="Placement" Value="Bottom" TargetName="SubMenuPopup"/>
                            <Setter Property="MinWidth" Value="0" TargetName="Col0"/>
                            <Setter Property="Width" Value="Auto" TargetName="Col3"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="GlyphPanel"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="IGTHost"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/>
                        </Trigger>-->

                        <!-- Role = TopLevelItem :  this is a child menu item from the top level without any child items-->
                        <Trigger Property="Role" Value="TopLevelItem">
                            <Setter Property="Padding" Value="6,1,6,1"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/>
                        </Trigger>

                        <!-- Role = SubMenuHeader : this is a child menu item which does not have children -->
                        <Trigger Property="Role" Value="SubmenuHeader">
                            <Setter Property="DockPanel.Dock" Value="Top"/>
                            <Setter Property="Padding" Value="0,2,0,2"/>
                        </Trigger>

                        <!-- Role = SubMenuItem : this is a child menu item which has children-->
                        <Trigger Property="Role" Value="SubmenuItem">
                            <Setter Property="DockPanel.Dock" Value="Top"/>
                            <Setter Property="Padding" Value="0,2,0,2"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/>
                        </Trigger>

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

                        <!-- If no Icon is present the we collapse the Icon Content -->
                        <Trigger Property="Icon" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/>
                        </Trigger>

                        <!-- The GlyphPanel contains the CheckMark -->
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="Visibility" Value="Visible" TargetName="GlyphPanel"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/>
                        </Trigger>

                        <!-- Using the system colors for the Menu Highlight and IsEnabled-->
                        <Trigger Property="IsHighlighted" Value="true">
                            <Setter Property="Background" Value="{StaticResource HighlightedBackgroundBrush}" TargetName="Border"/>
                            <Setter Property="Foreground" Value="{StaticResource HighlightedText}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}" TargetName="Border"/>
                        </Trigger>
                        <Trigger Property="IsHighlighted" Value="false">
                            <Setter Property="Background" Value="{StaticResource MenuBackgroundBrush}" TargetName="Border"/>
                            <Setter Property="Foreground" Value="{StaticResource SolidMenuFontBrush}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource MenuBackgroundBrush}" TargetName="Border"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="LightGray"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<UserControl.Foreground>
    <SolidColorBrush />
</UserControl.Foreground>
<Grid Height="24" Width="250" HorizontalAlignment="Left">
    <Grid.Background>
        <SolidColorBrush />
    </Grid.Background>
    <Menu Name="PrincipalMenu" Margin="0,0,-100,0">
    </Menu>
</Grid>

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Underscores are used in WPF Content controls as accelerator keys. 下划线在WPF内容控件中用作加速键。

So adding "E_dit" to a content control causes the "d" to be underlined at runtime. 因此,将“ E_dit”添加到内容控件会导致“ d”在运行时加下划线。 The user can press ALT-D to move focus to the control. 用户可以按ALT-D将焦点移到控件上。

In your case "Test_Header_Name" it's the "H" is prefixed by the underscore, so it is the accelerator key. 在您的情况下,“ Test_Header_Name”是“ H”的下划线前缀,因此它是加速键。

The solution is to use two underscores "__" to stop the accelerator key addition. 解决方案是使用两个下划线“ __”停止加速键的添加。

My guess is that the first underscore is being turned into an AccessKey for the MenuItem since you are using it in the Header . 我的猜测是,因为您在Header中使用了下划线,所以第一个下划线已变成MenuItem的AccessKey。 So in the example above, you probably have an AccessKey of 'h' for your MenuItem . 因此,在上面的示例中,您的MenuItemAccessKey可能为'h'。 See this answer for more on the subject. 有关主题的更多信息,请参见此答案

I'm not sure if there's a way to override the behavior, perhaps if you use the ascii code for the underscore or something. 我不确定是否有一种方法可以覆盖该行为,也许您是否将ascii代码用于下划线或其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM