c# - Grouping of WPF-CustomControl, which inherits from RadioButton, just works with GroupName -


i have made in wpf-customcontrollibrary customcontrol, inherits radiobutton.

in constructor of customcontrol customradiobutton override dependyproperty ischeckedproperty default value (in case false).

/// <summary> /// constructor. /// </summary> static customradiobutton() {     defaultstylekeyproperty.overridemetadata(typeof(customradiobutton), new frameworkpropertymetadata(typeof(customradiobutton)));      // override default value of base-dependencyproperty.     ischeckedproperty.overridemetadata(typeof(customradiobutton), new frameworkpropertymetadata(false)); } 

as long set in application, use customcontrollibrary, property groupname of radiobuttons, grouping mechanism works.

if 1 radiobutton enabled after click, others (which belong same group) disabled.

but if don't set property groupname, grouping doesn't work anymore.

if click on radiobutton, enabled forever.

how can grouping garanteed without explicit setting of property groupname?

thanks in advance!

and here definition of customcontrol in generic.xaml:

<!--style customcontrol customradiobutton--> <style targettype="{x:type local:customradiobutton}" basedon="{staticresource {x:type radiobutton}}">     <setter property="template">         <setter.value>             <controltemplate targettype="{x:type local:customradiobutton}">                 <border borderbrush="{templatebinding borderbrush}"                         borderthickness="{templatebinding borderthickness}">                     <radiobutton ischecked="{templatebinding ischecked}"                                  groupname="{templatebinding groupname}"                                  horizontalcontentalignment="{templatebinding horizontalcontentalignment}"                                  verticalcontentalignment="{templatebinding verticalcontentalignment}">                         <textblock text="{templatebinding text}"                                    textwrapping="wrap"                                    textalignment="{binding relativesource={relativesource mode=findancestor, ancestortype={x:type radiobutton}},                             path=horizontalcontentalignment, converter={staticresource h2talignmentconverter}}"                                    textdecorations="{templatebinding textdecorations}"/>                     </radiobutton>                 </border>             </controltemplate>         </setter.value>     </setter> </style> 

and code customcontrol:

using system.windows; using system.windows.controls;  namespace wpfdesignercustomcontrollibrary {     /// <summary>     /// class customcontrol inherits radiobutton , has nested textblock textwrapping.     /// </summary>     public class customradiobutton : radiobutton     {         /// <summary>         /// constructor.         /// </summary>         static customradiobutton()         {             defaultstylekeyproperty.overridemetadata(typeof(customradiobutton), new frameworkpropertymetadata(typeof(customradiobutton)));              // override default value of base-dependencyproperty.             ischeckedproperty.overridemetadata(typeof(customradiobutton), new frameworkpropertymetadata(false));         }          /// <summary>         /// dependencyproperty access text value.         /// </summary>         public static readonly dependencyproperty textproperty =             dependencyproperty.register("text", typeof(string), typeof(customradiobutton));          /// <summary>         /// dependencyproperty textdecorations.         /// </summary>         public static readonly dependencyproperty textdecorationsproperty =             dependencyproperty.register("textdecorations", typeof(textdecorationcollection), typeof(customradiobutton));          /// <summary>         /// dependencyproperty external access property tablename.         /// </summary>         public static readonly dependencyproperty tablenameproperty =             dependencyproperty.register("tablename", typeof(string), typeof(customradiobutton));          /// <summary>         /// gets or sets content.         /// </summary>         public string text         {             { return (string)getvalue(textproperty); }             set { setvalue(textproperty, value); }         }          /// <summary>         /// gets or sets textdecorations.         /// </summary>         public textdecorationcollection textdecorations         {             { return (textdecorationcollection)getvalue(textdecorationsproperty); }             set { setvalue(textdecorationsproperty, value); }         }          /// <summary>         /// gets or sets name of datatable datasource.         /// </summary>         public string tablename         {             { return (string)getvalue(tablenameproperty); }             set { setvalue(tablenameproperty, value); }         }     } } 

meanwhile decided override groupnameproperty default value. works usual , users have possibility change groupname, works multiple groups of radiobuttons.

    /// <summary>     /// constructor.     /// </summary>     static customradiobutton()     {         defaultstylekeyproperty.overridemetadata(typeof(customradiobutton),        new frameworkpropertymetadata(typeof(customradiobutton)));          [...]          // override default value of base-dependencyproperty groupname, radiobuttons synchronized usual.         // usage of multiple groups property groupname has set explicitly - usual.         groupnameproperty.overridemetadata(typeof(customradiobutton),        new frameworkpropertymetadata("group0"));     } 

supplement: instead of groupname "group0" above can use blank (" "). let radiobuttons work usual. empty string groupname doesn't work.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -