c# - Custom Button with a Viewbox inside -
i have style:
<style targettype="{x:type local:imagebutton}" basedon="{staticresource {x:type button}}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type local:imagebutton}"> <border> <stackpanel orientation="horizontal"> <viewbox x:name="viewboxinternal" child="{templatebinding child}"/> <textblock x:name="textblockinternal" text="{templatebinding text}" /> </stackpanel> </border> </controltemplate> </setter.value> </setter> </style>
and have 2 dependency properties.
static imagebutton() { defaultstylekeyproperty.overridemetadata(typeof(imagebutton), new frameworkpropertymetadata(typeof(imagebutton))); childproperty = dependencyproperty.register("child", typeof(uielement), typeof(imagebutton), new frameworkpropertymetadata()); textproperty = dependencyproperty.register("text", typeof(string), typeof(imagebutton), new frameworkpropertymetadata("button")); }
i can set text
property without problem, somehow can't set child of viewbox
. viewbox
should receive canvas
child
.
calling way, gives me error:
<custom:imagebutton text="new" width="41" child="{staticresource newicon}"/>
unable cast object of 'system.windows.templatebindingexpression' type 'system.windows.uielement'.
now, working...
as @xaml-lover said, need this:
<viewbox x:name="viewboxinternal"> <contentpresenter contentsource="{templatebinding content}" width="auto" height="auto"/> </viewbox>
and need call this:
<custom:imagebutton text="new" content="{staticresource newicon}" />
this because, child property of viewbox not dependency property. binding can set dependency property. suggest follow different approach. use contentpresenter place holder child property , wrap in a viewbox.
<viewbox x:name="viewboxinternal"> <contentpresenter contentsource="child"/> </viewbox>
Comments
Post a Comment