c# - Change default bitmap in CheckBox -
i find solution setter.targetname not work against element basedon setting. not understand how prement him me. have class:
public class mycheckbox: checkbox { public bool defaultvalue { { return (bool)getvalue(defaultvalueproperty); } set { setvalue(defaultvalueproperty, value); } } public static readonly dependencyproperty defaultvalueproperty = dependencyproperty.register("defaultvalue", typeof(bool), typeof(mycheckbox), new uipropertymetadata(false)); protected override void ontoggle() { this.ischecked = this.ischecked == null ? !defaultvalue : this.ischecked.value ? false : true; } }
and xaml
<style targettype="{x:type checkbox}"> <style.resources> <imaging:bitmapimage x:key="checkboxstatussource" urisource="falseifnull.png"/> <!-- icon if [ischecked] [null] --> </style.resources> <setter property="template"> <setter.value> <controltemplate targettype="{x:type checkbox}"> <image x:name="checkboxstatus" source="{dynamicresource checkboxstatussource}"/> <controltemplate.triggers> <trigger property="ischecked" value="true"> <setter targetname="checkboxstatus" property="source" value="b.png"/><!-- icon if [ischecked] [true] --> </trigger> <trigger property="ischecked" value="false"> <setter targetname="checkboxstatus" property="source" value="c.png"/><!-- icon if [ischecked] [false] --> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> <style targettype="{x:type my:mycheckbox}" basedon="{staticresource {x:type checkbox}}"> <style.triggers> <trigger property="defaultvalue" value="true"> <!-- !!!!!!!!!!!!!!this need change [urisource] in [checkboxstatussource] "trueifnull.png"!!!!!!!!!!!!!! --> </trigger> </style.triggers> </style>
perhaps there solution
unless need extend checkbox
other reasons, you're trying here can done attached properties too, , allow have 1 single style
checkboxes.
public static class checkboxextensions { public static void setdefaultvalue(checkbox element, bool value) { element.setvalue(defaultvalueproperty, value); } public static bool getdefaultvalue(checkbox element) { return (bool)element.getvalue(defaultvalueproperty); } // using dependencyproperty backing store defaultvalue. enables animation, styling, binding, etc... public static readonly dependencyproperty defaultvalueproperty = dependencyproperty.registerattached("defaultvalue", typeof(bool), typeof(checkboxextensions), new uipropertymetadata(false)); }
this property available use in checkbox
in solution, , in style , template use checkboxes. in case...
<style targettype="{x:type checkbox}"> <style.resources> <imaging:bitmapimage x:key="checkboxstatussourcefalse" urisource="falseifnull.png"/> <!-- icon if [ischecked] [null] , [defaultvalue] [false] --> <imaging:bitmapimage x:key="checkboxstatussourcetrue" urisource="trueifnull.png"/> <!-- icon if [ischecked] [null] , [defaultvalue] [true] --> </style.resources> <setter property="template"> <setter.value> <controltemplate targettype="{x:type checkbox}"> <image x:name="checkboxstatus" source="{dynamicresource checkboxstatussourcefalse}"/> <controltemplate.triggers> <multitrigger> <multitrigger.conditions> <condition property="local:checkboxextensions.defaultvalue" value="true" /> <condition property="ischecked" value="{x:null}" /> </multitrigger.conditions> <setter targetname="checkboxstatus" property="source" value="{dynamicresource checkboxstatussourcefalse}"/> </multitrigger> <trigger property="ischecked" value="true"> <setter targetname="checkboxstatus" property="source" value="b.png"/><!-- icon if [ischecked] [true] --> </trigger> <trigger property="ischecked" value="false"> <setter targetname="checkboxstatus" property="source" value="c.png"/><!-- icon if [ischecked] [false] --> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style>
edit: if still need extend checkbox
, style work both regular checkbox
, mycheckbox
implementation. create style that's based on one.
<style targettype="{x:type my:mycheckbox}" basedon="{staticresource {x:type checkbox}}"> ... </style>
and if want access defaultvalue
property code-behind, can way:
checkboxextensions.setdefaultvalue(checkbox, true); bool defaultvalue = checkboxextensions.getdefaultvalue(checkbox);
Comments
Post a Comment