wpf - BarButtonItems and BarSubItems on Bound RibbonControl -
i developing wpf application using devexpress controls, such ribbon control. want able place buttons on ribbon dynamically. able support both regular buttons , drop-down buttons.
i thinking similar below.
wpf view:
<usercontrol.resources> <datatemplate x:key="ribboncommandtemplate"> <contentcontrol> <dxb:barbuttonitem ribbonstyle="all" content="{binding caption}" command="{binding (dxr:ribboncontrol.ribbon).datacontext.menuexecutecommand, relativesource={relativesource self}}" commandparameter="{binding}" /> </contentcontrol> </datatemplate> </usercontrol.resources> <grid> <dockpanel> <dxr:ribboncontrol dockpanel.dock="top" ribbonstyle="office2010"> <dxr:ribbondefaultpagecategory> <dxr:ribbonpage caption="home"> <dxr:ribbonpagegroup caption="dynamic commands" itemlinkssource="{binding dynamiccommands}" itemtemplate="{staticresource ribboncommandtemplate}" /> </dxr:ribbonpage> </dxr:ribbondefaultpagecategory> </dxr:ribboncontrol> <grid/> </dockpanel> </grid>
view model:
public class ribboncommand { public string caption { get; set; } public int commandcode { get; set; } public observablecollection<ribboncommand> subitems { get; set; } public bool hassubitems { { if (subitems != null) return (subitems.count > 0); else return false; } } } [pocoviewmodel] public class mainviewmodel { public observablecollection<ribboncommand> dynamiccommands { get; set; } public mainviewmodel() { dynamiccommands = new observablecollection<ribboncommand>(); // regular buttons. dynamiccommands.add(new ribboncommand() { caption = "button 1", commandcode = 1 }); dynamiccommands.add(new ribboncommand() { caption = "button 2", commandcode = 2 }); // drop-down button. ribboncommand dropdowncommand = new ribboncommand() { caption = "drop-down", commandcode = 3 }; dropdowncommand.subitems = new observablecollection<ribboncommand>(); dropdowncommand.subitems.add(new ribboncommand() { caption = "sub-item 1", commandcode = 31 }); dropdowncommand.subitems.add(new ribboncommand() { caption = "sub-item 2", commandcode = 32 }); dropdowncommand.subitems.add(new ribboncommand() { caption = "sub-item 3", commandcode = 33 }); dynamiccommands.add(dropdowncommand); } public void menuexecute(ribboncommand command) { messagebox.show(string.format("you clicked command id: {0} (\"{1}\").", command.commandcode, command.caption), "bound ribbon control"); } }
this code populate ribbon items added in dynamiccommands collection, support drop-down buttons items in subitems collection (the third button on example above).
is there way conditionally change type of control displayed in datatemplate. if object's hassubitems true, barsubitem placed on ribbon. if false, keep barbuttonitem.
if regular wpf rather uwp, , if datacontexts of subitems of different types, can define multiple datatemplates
datatype
attributes in ribbonpagegroup's resources (where won't in scope doesn't need them), , rid of itemtemplate
attribute:
<dxr:ribbonpagegroup caption="dynamic commands" itemlinkssource="{binding dynamiccommands}"> <dxr:ribbonpagegroup.resources> <datatemplate datatype="{x:type local:ribboncommand}"> <!-- xaml stuff --> </datatemplate> <datatemplate datatype="{x:type local:specialribboncommand}"> <!-- totally different xaml stuff --> </datatemplate> </dxr:ribbonpagegroup.resources> <!-- etc -->
for option, should able write datatemplateselector
, give ribboncontrol
's toolbaritemtemplateselector
property or ribbonpagegroup
's itemtemplateselector
property.
lastly, write 1 complicated datatemplate
multiple child controls superimposed in grid
, , series of triggers show appropriate 1 based on properties of datacontext
. if you've got 2 different options handle, may quickest , easiest route.
<datatemplate x:key="ribboncommandtemplate"> <grid> <label x:name="onething" /> <label x:name="anotherthing" /> </grid> <datatemplate.triggers> <datatrigger binding="{binding hassubitems}" value="true"> <setter targetname="onething" property="visibility" value="collapsed" /> <setter targetname="anotherthing" property="visibility" value="visible" /> </datatrigger> <!-- other triggers hassubitems == false, whatever --> </datatemplate.triggers> </datatemplate>
this seems pretty crude, i've done in wpf i'm getting desensitized it.
Comments
Post a Comment