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}" /> 

voÀla

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

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -