Note that there are some explanatory texts on larger screens.

plurals
  1. POCopying an ElementName binding
    primarykey
    data
    text
    <p>I have a TextBox (TB1) which has its Text value bound to another TextBox (TB2) using ElementName="TB2" and Path="Text". </p> <p>I then have a third TextBox (TB3) which I am SetBinding in the code behind to TB1 which I had hoped would allow me edit TB3 and both TB1 &amp; TB2 would also reflect the changes due to the binding being (theoretically) the same for all.</p> <p>I can edit TB1 and TB2 is updated (and vice versa), but TB3 never displays / updates the value.</p> <p>I can only think it's because TB1 binding is using ElementName and not a DependencyProperty? </p> <p>Is it possible to copy the binding of an element bound using ElementName?</p> <pre><code>&lt;TextBox Name="TB1"&gt; &lt;Binding ElementName="TB2" Path="Text" UpdateSourceTrigger="PropertyChanged"/&gt; &lt;/TextBox&gt; &lt;TextBox Name="TB2"&gt; &lt;Binding Path="Name" UpdateSourceTrigger="PropertyChanged"/&gt; &lt;/TextBox&gt; </code></pre> <p>Then in code behind I have:</p> <pre><code>BindingExpression bindExpression = TB1.GetBindingExpression(TextBox.TextProperty); if (bindExpression != null &amp;&amp; bindExpression.ParentBinding != null &amp;&amp; bindExpression.ParentBinding.Path != null) { TB3.DataContext = TB1.DataContext; TB3.SetBinding(TextBox.TextProperty, bindExpression.ParentBinding); } </code></pre> <hr> <p>Typically just found with testing just now that this does work if within the same xaml. However, I have TB3 in its own window as below, and the textbox is never bound correctly.. what am I missing?</p> <pre><code>if (bindExpression != null &amp;&amp; bindExpression.ParentBinding != null &amp;&amp; bindExpression.ParentBinding.Path != null) { PopupWindow wnd = new PopupWindow(); wnd.DataContext = TB1.DataContext; wnd.TB3.SetBinding(TextBox.TextProperty, bindExpression.ParentBinding); wnd.Show(); } </code></pre> <hr> <p>I'm not 100% sure why but this did the trick, it seems to do the same binding as SetBinding but with the source being set to the DataItem, but it gave me the required result... Thanks Klaus78..</p> <pre><code>if (bindExpression != null &amp;&amp; bindExpression.ParentBinding != null &amp;&amp; bindExpression.ParentBinding.Path != null) { PopupWindow wnd = new PopupWindow(); Binding b = new Binding(); b.Source = bindExpression.DataItem; //TB1; b.Path = new PropertyPath("Text"); b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; wnd.TB3.SetBinding(TextBox.TextProperty, b); wnd.Show(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload