Creating System.Windows.Controls and adding Two Way Binding of a Field to a UserControle in WPF -
what want following
create usercontrole this:
public partial class myusercontrole : usercontrol
with wrappanel inside add controls , stuff to
<wrappanel margin="0,0,0,41" name="wrappanel1" />
this class has 2 buttons cancel or apply settings in window. window later gets open this
window w = new window(); w.sizetocontent = system.windows.sizetocontent.widthandheight; w.content = myclass.getmyusercontrole(); bool? t = w.showdialog();
where myclass special class creates usercontrole , adds stuff wants changed in wrappanel
now in myclass have simple point
system.drawing.point mypoint = new point(10,15);
and getmyusercontrole() looks like
public myusercontrole getmyusercontrole(){ myusercontrole uc = new myusercontrole(); wpf.textbox x1 = new wpf.textbox(); system.windows.data.binding bindingx = new system.windows.data.binding("x"); bindingx.source = mypoint; bindingx.mode = system.windows.data.bindingmode.twoway; x1.setbinding(wpf.textbox.textproperty, bindingx); uc.addhere.add(x1); return uc; }
the textbox filled changes dont change source. how can fix this?
edit: can add
private myusercontrole myucsave = null;
to class , set
myucsave = uc;
at end of getmyusercontrole() , check @ start
myucsave != null return myucsave;
that fixes "save" of textbox, underlying mypoint doesnt change.
edit: maybe there more simple case: create simple form , add mainwindow via constructor
public partial class mainwindow : window public mainwindow() { initializecomponent(); textbox x1 = new textbox(); textbox y1 = new textbox(); x1.margin = new thickness(0, 0, 20, 20); y1.margin = new thickness(0, 0, 100, 100); x1.width = 100; y1.width = 100; x1.height = 200; y1.height = 200; system.windows.data.binding bindingx = new system.windows.data.binding("x"); system.windows.data.binding bindingy = new system.windows.data.binding("x"); bindingx.mode = system.windows.data.bindingmode.twoway; bindingy.mode = system.windows.data.bindingmode.twoway; bindingx.source = mypoint; bindingy.source = mypoint; x1.setbinding(textbox.textproperty, bindingx); y1.setbinding(textbox.textproperty, bindingy); this.maingrid.children.add(y1); this.maingrid.children.add(x1); } point mypoint = new point(100, 200);
this should create 2 textboxes x1,y1 linked same source (mypoint.x)? when change 1 thing other textbox doesn't change neither source.
Comments
Post a Comment