Thursday 15 January 2015

c# - Fighting with binding -



c# - Fighting with binding -

i have checkboxes defined this:

(this checkbox can click on check/uncheck other checkboxes, let's phone call *)

<checkbox content="check all" grid.column="1" grid.row="1" margin="200 7 0 0" name="ischeckedcheckall" checked="ischeckedcheckall_checked" unchecked="ischeckedcheckall_checked" />

and c# code above, is:

if (ischeckedcheckall.ischecked == true) { ischeckedcheckall.content = "uncheck all"; } else { ischeckedcheckall.content = "check all"; }

(and below checkbox depends on 1 above, , have several others this, lets phone call **)

<checkbox name="exc2" grid.column="1" grid.row="3" margin="200 7 0 0" ischecked="{binding ischecked, elementname=ischeckedcheckall, mode=oneway}" />

the thing notice, binding. have 10 other checkboxes have registered binding. can check * checkbox, others have binding gets checked. let's want uncheck ** checkbox. still noted checked, value inserted in database wrong.

my c# code determining if checkbox checked looks this:

bool ex2 = (bool) exc2.ischecked

this value homecoming false, if exc1 checkbox indeed checked, checkbox binding unchecked. might seem easy fix, can't come it. can prepare if stop using bindings, want utilize binding. maybe i'm using binding in wrong way?

i tried explain problem can, hope it's understandable

things working expected me. see sample below:

class="snippet-code-html lang-html prettyprint-override"><window x:class="wpfapplication2.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <window.resources> <style x:key="somestyle"> <style.triggers> <trigger property="checkbox.ischecked" value="true"> <trigger.setters> <setter property="checkbox.content" value="uncheck all"></setter> </trigger.setters> </trigger> <trigger property="checkbox.ischecked" value="false"> <trigger.setters> <setter property="checkbox.content" value="check all"></setter> </trigger.setters> </trigger> </style.triggers> </style> </window.resources> <grid> <grid.rowdefinitions> <rowdefinition/> <rowdefinition/> <rowdefinition/> <rowdefinition/> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition/> <columndefinition/> </grid.columndefinitions> <checkbox grid.column="1" grid.row="0" margin="200 7 0 0" name="ischeckedcheckall" style="{staticresource somestyle}"/> <checkbox name="exc2" grid.column="1" grid.row="1" margin="200 7 0 0" ischecked="{binding ischecked, elementname=ischeckedcheckall, mode=oneway}" /> <checkbox name="exc3" grid.column="1" grid.row="2" margin="200 7 0 0" ischecked="{binding ischecked, elementname=ischeckedcheckall, mode=oneway}" /> <button grid.column="1" grid.row="3" content="{binding ischecked, elementname=ischeckedcheckall}" click="buttonbase_onclick"></button> </grid> </window> namespace wpfapplication2 { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); } private void buttonbase_onclick(object sender, routedeventargs e) { messagebox.show(((bool) exc2.ischecked).tostring()); } } }

also can utilize styles , triggers auto alter text * checkbox.

c# wpf

No comments:

Post a Comment