c# - How to fit WPF MenuItem's size based on it child's size -


i have context menu. 1 of menuitems contains combobox. when menu item w/ combobox rendered menuitem wider combo box. snap shot w/ issue ideas how can set menu item's size snaged combobox. here how create menu , combobox:

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 recipient"}; removemenu.click += (sender, e) => removerecipient(); removemenu.isenabled = actionset.actions.count > 1 || recipient != null;  menu.items.add(removemenu); 

i tried set rendersize in runtime zero.

thank you.

here link menuitem controltemplate example microsoft. can edit 1 obtain desired aspect. example in 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> 

you can edit grid.collumndefinitions resize or remove columns if don't need them. if add controltemplate link in separate resourcedictionary (which recommend do), don't forget add in mergedictionaries app.xaml file make visible in entire application. here how if add directly in root of app:

 <application.resources>     <resourcedictionary>         <resourcedictionary.mergeddictionaries>             <resourcedictionary source="/yourapplicationname;component\menustyle.xaml"/>         </resourcedictionary.mergeddictionaries>     </resourcedictionary> </application.resources> 

i hope might solve problem.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -