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
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>
在.cs文件的构造函数中,尝试以下操作:
this.DataContext = this;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.