如何根据孩子的大小调整WPF MenuItem的大小

时间:2013-04-05 09:11:22

标签: c# wpf combobox menuitem

我有上下文菜单。其中一个菜单包含组合框。 当菜单项w / combobox被渲染时,menuitem比组合框宽得多。 Snap shot w/ the issue 任何想法如何我可以设置菜单项的大小被打到组合框。 以下是我创建菜单和组合框的方法:

XamComboEditor combo = new XamComboEditor();
combo.ItemsSource = Context.RecipientOptions;
combo.DisplayMemberPath = "Display";
combo.IsEditable = true;
combo.MinWidth = 250;
combo.DropDownResizeMode = PopupResizeMode.None;
combo.HorizontalAlignment = HorizontalAlignment.Stretch;
combo.LimitToList = false;
combo.SetBinding(XamComboEditor.SelectedItemProperty, new Binding("Recipient") 
                 {Source = this, Mode = BindingMode.TwoWay});
MenuItem comboMenu = new MenuItem {Header = combo, StaysOpenOnClick = true, 
                                   RenderSize = combo.RenderSize,};
menu.Items.Add(comboMenu);          
MenuItem removeMenu = new MenuItem {Header = "Remove this recipient"};
removeMenu.Click += (sender, e) => RemoveRecipient();
removeMenu.IsEnabled = ActionSet.Actions.Count > 1 || Recipient != null;

menu.Items.Add(removeMenu);

我尝试设置RenderSize,但在运行时它是零。

谢谢。

1 个答案:

答案 0 :(得分:1)

Here是来自Microsoft的MenuItem ControlTemplate示例的链接。您可以编辑此一个以获得所需的方面。 例如,在这个xaml中:

<ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="MenuItem">
    <Border Name="Border" >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
                <ColumnDefinition Width="13"/>
            </Grid.ColumnDefinitions>
            <ContentPresenter Name="Icon" Margin="6,0,6,0" VerticalAlignment="Center" ContentSource="Icon"/>
            <Border Name="Check"  
                    Width="13" Height="13" 
                    Visibility="Collapsed"
                    Margin="6,0,6,0" 
                    Background="{StaticResource NormalBrush}"
                    BorderThickness="1"
                    BorderBrush="{StaticResource NormalBorderBrush}">
                <Path Name="CheckMark"
                      Width="7" Height="7" 
                      Visibility="Hidden" 
                      SnapsToDevicePixels="False" 
                      Stroke="{StaticResource GlyphBrush}"
                      StrokeThickness="2"
                      Data="M 0 0 L 7 7 M 0 7 L 7 0" />
            </Border>
            <ContentPresenter Name="HeaderHost"
                              Grid.Column="1"
                              ContentSource="Header"
                              RecognizesAccessKey="True"/>
            <TextBlock x:Name="InputGestureText"
                        Grid.Column="2"
                        Text="{TemplateBinding InputGestureText}"
                        Margin="5,2,0,2"
                        DockPanel.Dock="Right" />
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="Icon" Value="{x:Null}">
            <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="true">
            <Setter TargetName="CheckMark" Property="Visibility" Value="Visible"/>
        </Trigger>
        <Trigger Property="IsCheckable" Value="true">
            <Setter TargetName="Check" Property="Visibility" Value="Visible"/>
            <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
        </Trigger>
        <Trigger Property="IsHighlighted" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

如果您不需要,可以编辑Grid.CollumnDefinitions以调整大小甚至删除一些列。 如果您在单独的ResourceDictionary(我建议您这样做)中添加来自链接的controlTemplate,请不要忘记在App.xaml文件的MergeDictionaries中添加它,以使其在整个应用程序中可见。 如果您直接在应用的根目录中添加它,请按照以下步骤操作:

 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/YourApplicationName;component\MenuStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

我希望这可以帮助您解决问题。