xaml - WPF - Radio button control template binding "IsChecked" not working -
i have control template below, , want ischecked
property when user selects radio button.
but when user select radio button "a" it's ischecked
property still show false. why?
<controltemplate x:key="radiobtntemplate" targettype="{x:type radiobutton}"> <grid> <stackpanel margin="5"> <radiobutton name="tempbtn" ischecked="{templatebinding ischecked}" fontfamily="segoe ui" fontsize="18.667" content="{templatebinding content}" groupname="{templatebinding groupname}"/> </stackpanel> </grid> </controltemplate>
and use template:
<radiobutton groupname="cg" x:name="_rdobtna" content="a" template="{dynamicresource radiobtntemplate}" ischecked="true"/> <radiobutton groupname="cg" x:name="_rdobtnb" content="b" template="{dynamicresource radiobtntemplate}" /> <radiobutton groupname="cg" x:name="_rdobtnc" content="c" template="{dynamicresource radiobtntemplate}" />
if take example have 2 problems cause problems seeing.
issue 1: firstly design has created six not three <radiobutton>
controls. 3 in <stackpanel>
, 3 created part of control template. six radio buttons linked part of groupname="cg"
group.
as know because belong same cg
group 1 of 6 radio buttons can have ischecked
property set true
. 3 named controls _rdobtna
, _rdobtnb
, _rdobtnc
not visible on screen can never set true
(and in case of _rdobtna
promptly set false
xaml
declared true
moment template control bound).
to resolve situation, remove groupname="{templatebinding groupname}"
control template definition leaving 3 top level radio buttons in group.
issue 2: issue thought root of problem begin with. ischecked={templatebinding ischecked}
oneway
binding , not update other way. make binding twoway
need use long-hand version of binding definition, ischecked="{binding ischecked, relativesource={relativesource templatedparent}, mode=twoway}"
the control template becomes making 2 changes.
<controltemplate x:key="radiobtntemplate" targettype="{x:type radiobutton}"> <grid> <stackpanel margin="5"> <radiobutton name="tempbtn" ischecked="{binding ischecked, relativesource={relativesource templatedparent}, mode=twoway}" fontfamily="segoe ui" fontsize="18.667" content="{templatebinding content}" /> </stackpanel> </grid> </controltemplate>
Comments
Post a Comment