Search code examples
wpf2-way-object-databinding

Why in this case DataBinding doesn't work vice versa?


First one has the desired affect - change text in textbox and the content of the label changes whereas the second one doesn't. Why?

 <StackPanel>
        <Label Name="displayText" Content="{Binding ElementName=displayText, Path=Content, Mode=TwoWay}"/>
        <TextBox Name="sourceInfo"/>
    </StackPanel>

<StackPanel>
        <Label Name="displayText"/>
        <TextBox Name="sourceInfo" Text="{Binding ElementName=displayText, Path=Content, Mode=TwoWay}"/>
    </StackPanel>

Solution

  • These two work:

      <StackPanel>
         <Label Name="displayText2" 
                Content="{Binding ElementName=sourceInfo2, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
         <TextBox Name="sourceInfo2"/>
      </StackPanel>
    
      <StackPanel>
         <Label Name="displayText"/>
         <TextBox Name="sourceInfo" 
                  Text="{Binding ElementName=displayText, Path=Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
      </StackPanel>
    

    You need to bind to the correct property on the correct item. In the top case, you bount the label to its own Content property... oops.