views:

53

answers:

2

What should be one of the most simpliest form of databinding, is now causing me great amounts of grief. In my XAML, I have something as such:

<TextBox Text="{Binding Path=Speed}" />

Then in my matching .cs file, I have a property as such:

public int Speed { get; set; } 

But they aren't binding in harmony and I don't understand why. >:-( Any suggestions as to what I'm doing wrong would be greatly appreciated! Many thanks in advance! :-D

+2  A: 

The source for binding by default is the DataSource of the control or first ancestor that have a one set. Since the control DataSource is probably not the control itself the binding fails.

It can be solved by giving a name to the UserControl (or Window...) and setting the binding with ElementName.

For example:

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Name="myWindow">
   <Grid>
       <TextBox Text="{Binding Path=Speed, ElementName=myWindow}" />
   </Grid>
</Window>
Elisha
:-O you're my hero! thanks! :D that did it. :-) (7 minutes and I can accept this :-) )
townsean
+2  A: 

In the constructor in your .cs file try this:

this.DataContext = this;
Catalin DICU
ah, nice...that works too. :-) Thanks!
townsean