Two Forms Binding WPF -


i'm still learning wpf , tried make simple score program use 2 form. first form control panel, , second 1 display. couldn't find way change value in display form when change value in control panel. googled it, said use binding. me please? here code:

<window x:class="score.control"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:local="clr-namespace:score"     mc:ignorable="d"     title="control" height="300" width="400" minheight="300" minwidth="400" background="#ff181818" windowstartuplocation="centerscreen"> <grid>     <grid.rowdefinitions>         <rowdefinition height="1*"/>         <rowdefinition height="4*"/>     </grid.rowdefinitions>     <grid grid.row="0">         <grid.rowdefinitions>             <rowdefinition height="3*"/>             <rowdefinition height="2*"/>         </grid.rowdefinitions>         <viewbox grid.row="0">             <textbox x:name="txtjudul1" foreground="#ff00aade">judul 1</textbox>         </viewbox>         <viewbox grid.row="1">             <textbox x:name="txtjudul2" foreground="#ffc6c6c6">judul 2</textbox>         </viewbox>     </grid>     <grid grid.row="1">         <grid.columndefinitions>             <columndefinition/>             <columndefinition width="0.5*"/>             <columndefinition/>         </grid.columndefinitions>         <dockpanel grid.column="0">             <grid>                 <grid.rowdefinitions>                     <rowdefinition/>                     <rowdefinition height="4*"/>                     <rowdefinition/>                 </grid.rowdefinitions>                 <viewbox grid.row="0" margin="2">                     <textbox x:name="txtnamaa" foreground="#ff8a9b0f">a</textbox>                 </viewbox>                 <viewbox grid.row="1">                     <textbox x:name="txtscorea" foreground="#fffca910">1</textbox>                 </viewbox>                 <dockpanel horizontalalignment="center" grid.row="2" margin="5">                     <viewbox>                         <button x:name="btntambaha">+</button>                     </viewbox>                     <viewbox>                         <button x:name="btnkuranga">-</button>                     </viewbox>                 </dockpanel>             </grid>         </dockpanel>         <grid grid.column="1">             <grid.rowdefinitions>                 <rowdefinition height="1*"/>                 <rowdefinition height="3*"/>                 <rowdefinition height="1*"/>             </grid.rowdefinitions>             <viewbox>                 <button x:name="btndisplay" grid.row="0" margin="0,5" click="btndisplay_click">                     display</button>             </viewbox>             <grid grid.row="1">                 <grid.rowdefinitions>                     <rowdefinition/>                     <rowdefinition/>                 </grid.rowdefinitions>                 <viewbox grid.row="0">                     <textblock foreground="#ffc6c6c6"> vs </textblock>                 </viewbox>                 <grid grid.row="1" margin="5">                     <grid.columndefinitions>                         <columndefinition/>                         <columndefinition/>                         <columndefinition/>                     </grid.columndefinitions>                     <viewbox grid.column="0">                         <textbox x:name="txtpointa"  foreground="#fff14d0f">x</textbox>                     </viewbox>                     <viewbox grid.column="1">                         <textblock foreground="#ffc6c6c6">-</textblock>                     </viewbox>                     <viewbox grid.column="2">                         <textbox x:name="txtpointb" foreground="#fff14d0f">y</textbox>                     </viewbox>                 </grid>             </grid>             <wrappanel horizontalalignment="center" grid.row="2" margin="6">                 <viewbox>                     <button x:name="btnreset" height="20">r</button>                 </viewbox>                 <viewbox>                     <button name="btnswitch">s</button>                 </viewbox>             </wrappanel>         </grid>         <dockpanel grid.column="2">             <grid>                 <grid.rowdefinitions>                     <rowdefinition/>                     <rowdefinition height="4*"/>                     <rowdefinition/>                 </grid.rowdefinitions>                 <viewbox grid.row="0" margin="2">                     <textbox x:name="txtnamab" foreground="#ff8a9b0f">b</textbox>                 </viewbox>                 <viewbox grid.row="1">                     <textbox x:name="txtscoreb" foreground="#fffca910">0</textbox>                 </viewbox>                 <dockpanel horizontalalignment="center" grid.row="2" margin="5">                     <viewbox>                         <button x:name="btntambahb">+</button>                         </viewbox>                     <viewbox>                         <button x:name="btnkurangb">-</button>                     </viewbox>                 </dockpanel>             </grid>         </dockpanel>     </grid> </grid> 

<window x:class="score.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:local="clr-namespace:score"     mc:ignorable="d"     title="mainwindow" height="350" width="525" background="#ff181818"      windowstyle="none" resizemode="noresize" windowstate="maximized"> <grid>     <grid.rowdefinitions>         <rowdefinition height="1*"/>         <rowdefinition height="4*"/>     </grid.rowdefinitions>     <grid grid.row="0">         <grid.rowdefinitions>             <rowdefinition height="3*"/>             <rowdefinition height="2*"/>         </grid.rowdefinitions>         <viewbox grid.row="0">             <textblock x:name="txtjudul1" foreground="#ff00aade"> judul 1 </textblock>         </viewbox>         <viewbox grid.row="1">             <textblock foreground="#ffc6c6c6"> judul 2 </textblock>         </viewbox>     </grid>     <grid grid.row="1">         <grid.columndefinitions>             <columndefinition/>             <columndefinition width="0.5*"/>             <columndefinition/>         </grid.columndefinitions>         <dockpanel grid.column="0">             <grid>                 <grid.rowdefinitions>                     <rowdefinition/>                     <rowdefinition height="4*"/>                 </grid.rowdefinitions>                 <viewbox grid.row="0">                     <textblock foreground="#ff8a9b0f">a</textblock>                 </viewbox>                 <viewbox grid.row="1">                     <textblock foreground="#fffca910">1</textblock>                 </viewbox>             </grid>         </dockpanel>         <grid grid.column="1">             <grid.rowdefinitions>                 <rowdefinition height="1*"/>                 <rowdefinition height="3*"/>                 <rowdefinition height="1*"/>             </grid.rowdefinitions>             <grid grid.row="1">                 <grid.rowdefinitions>                     <rowdefinition/>                     <rowdefinition/>                 </grid.rowdefinitions>                 <viewbox grid.row="0">                     <textblock foreground="#ffc6c6c6">vs</textblock>                 </viewbox>                 <grid grid.row="1" margin="5">                     <grid.columndefinitions>                         <columndefinition/>                         <columndefinition/>                         <columndefinition/>                     </grid.columndefinitions>                     <viewbox grid.column="0">                         <textblock foreground="#fff14d0f">x</textblock>                     </viewbox>                     <viewbox grid.column="1">                         <textblock foreground="#ffc6c6c6">-</textblock>                     </viewbox>                     <viewbox grid.column="2">                         <textblock foreground="#fff14d0f">y</textblock>                     </viewbox>                 </grid>             </grid>         </grid>         <dockpanel grid.column="2">             <grid>                 <grid.rowdefinitions>                     <rowdefinition/>                     <rowdefinition height="4*"/>                 </grid.rowdefinitions>                 <viewbox grid.row="0">                     <textblock foreground="#ff8a9b0f">b</textblock>                 </viewbox>                 <viewbox grid.row="1">                     <textblock foreground="#fffca910">0</textblock>                 </viewbox>             </grid>         </dockpanel>     </grid> </grid> 

i have created sample application based on needs , uploaded here :

https://www.dropbox.com/s/4fiia3wpu8pnrkf/general.rar?dl=0

i have kept simple possible new wpf, , have skipped commands, mvvm etc.

few points regarding upload :

  1. you can extract , run without issues.

  2. in xaml code, have made changes show player names/scores. : <textbox x:name="txtnamaa" text="{binding item1.name,mode=twoway, updatesourcetrigger=propertychanged}" foreground="#ff8a9b0f"/>.

  3. this 2-way binding magic works because of inotifypropertychanged. our business entities presented below :

         using system;      using system.componentmodel;      using system.collections.generic;        namespace general.score       {          public class controlpanel           {               static tuple<playerscore, playerscore> _playerpairs;               public static tuple<playerscore, playerscore> playerpairs { { return _playerpairs; } }               static controlpanel()               {                   playerscore p1 = new playerscore() { name = "a", score = 0 };                  playerscore p2 = new playerscore() { name = "b", score = 0 };                   _playerpairs = new tuple<playerscore, playerscore>(p1, p2);                }            }             public class playerscore : inotifypropertychanged             {                 string _name;                 public string name { { return _name; } set { _name = value; onpropertychanged("name"); } }                      int _score;                 public int score { { return _score; } set { _score = value; onpropertychanged("score"); } }                  public event propertychangedeventhandler propertychanged;                  private void onpropertychanged(string p)                 {                    if (propertychanged != null)                        propertychanged(this, new propertychangedeventargs(p));                 }             } 

    }

see figure below : solution explanation


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -