c# - WPF TextBox 数据绑定(bind)问题 > :-(

标签 c# .net wpf data-binding

本应是最简单的数据绑定(bind)形式之一,现在却让我非常伤心。在我的 XAML 中,我有这样的东西:

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

然后在我匹配的 .cs 文件中,我有一个这样的属性:

public int Speed { get; set; } 

但它们并没有和谐地结合在一起,我不明白为什么。 >:-( 任何关于我做错了什么的建议将不胜感激!非常感谢!:-D

最佳答案

默认情况下,绑定(bind)源是控件的 DataSource 或具有 one 集的第一个祖先。由于控件 DataSource 可能不是控件本身,因此绑定(bind)失败。

可以通过为 UserControl(或 Window...)命名并使用 ElementName 设置绑定(bind)来解决。

例如:

<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>

关于c# - WPF TextBox 数据绑定(bind)问题 > :-(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3612804/

相关文章:

c# - 如何更改datagridview中的行颜色

c# - 套接字连接错误

.NET Rx Koans : Why does this test case fail?

c# - 获取当前节点的完整路径

.net - IntPtr.Size 显示操作系统版本还是 .NET 版本?

c# - 如何捕获和处理 PreviewMouseDownOutsideCapturedElementEvent?

c# - WPF 中的自定义内容控件

c# - 从另一个 WPF 应用程序加载 WPF 应用程序程序集,在同一 AppDomain 中获取错误 : Cannot create more than one System. Windows.Application 实例

c# - 在 C# 中使用 Using 的不同方式

c# - 创建 X509Certificate2 时阻止创建文件?