c# - What am I doing wrong so the data binding target doesn't get affected by data binding source -


i've moved windows forms wpf. started reed copsey's series 'better user , developer experiences – windows forms wpf mvvm'. on 4th part of series, following code should fill text boxes data:

<textbox grid.row="0" grid.column="1" text="{binding path=feed.title, mode=oneway}" /> <textbox grid.row="1" grid.column="1" text="{binding path=feed.link.absoluteuri, mode=oneway}" /> <textbox grid.row="2" grid.column="1" text="{binding path=feed.description, mode=oneway}"/> 

i tried use template-of-code 'update target (textblock.text) source (textbox.text) updates', , full xaml code:

<window x:class="wpfapplication1.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" height="608.134" width="768.284">     <grid>         <button content="button" horizontalalignment="left" height="28" margin="442,56,0,0" verticalalignment="top" width="139" click="button_click_1"/>         <textbox x:name="textbox1" horizontalalignment="left" height="28" margin="56,56,0,0" textwrapping="wrap" text="textbox1" verticalalignment="top" width="237"/>         <textblock horizontalalignment="left" height="66" margin="56,168,0,0" textwrapping="wrap" verticalalignment="top" width="285"                    text="{binding path=textbox1.text, mode = oneway}"/>      </grid> </window> 

the expected value text of textblock "textbox1" (textbox1.text), textblock text null!

so, checked what triggers source updates, , decided change binding mode twoway, got same result!

at last, found "how to: control when textbox text updates source" shows how that. according reed copsey said in part of series:

less code means less maintain, less test, , less worry about.

and according source found on msdn:

    <label>enter name:</label> <textbox>   <textbox.text>     <binding source="{staticresource mydatasource}" path="name"              updatesourcetrigger="propertychanged"/>   </textbox.text> </textbox>  <label>the name entered:</label> <textblock text="{binding source={staticresource mydatasource}, path=name}"/> 

i'll typing (roughly) same amount of code. , such mission (to change textblock textbox changes) can accomplished using normal event handlers. questions are:

  1. if same mission can accomplished same amount of code, makes wpf data binding special?
  2. what wrong first code?
  3. in above msdn code, had type xaml code both source , target. if wanted source value inside class, possible accomplish such mission? , how?

any appreciated, in advance.

you first attempt incorrect binding path relative datacontext of textblock. trying bind specific element, can use elementname specify source , path relative this:

text="{binding elementname=textbox1, path=text}" 

the idiomatic approach wpf use mvvm. in situation, both textbox , textblock bound property on view model.

the changing of text in textbox update property in turn update textblock. view model free wpf view concerns , can unit tested without involving wpf.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -