1

我有上下文菜单。menuitems 之一包含组合框。当呈现带有组合框的菜单项时,menuitem 比组合框宽得多。 有问题的快照 任何想法如何设置菜单项的大小以连接到组合框。这是我创建菜单和组合框的方法:

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 但在运行时它为零。

谢谢你。

4

1 回答 1

1

是来自 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>

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

于 2013-04-09T11:52:52.120 回答