一个简单的数据绑定无法正常工作,虽然我犯了一些错误,却无法弄清楚出了什么问题:
UserControl Xaml:
<UserControl x:Class="PurchaseStockControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Text="{Binding Path=Namee}" DataContext="Me"/>
</Grid>
</UserControl>
我想绑定属性名称:但结果是“System.Windows.Data错误:40:BindingExpression路径错误:在'对象'''字符串'(HashCode = -840583197)'上找不到'Namee'属性。 BindingExpression:Path = Namee; DataItem ='String'(HashCode = -840583197);目标元素是'TextBox'(Name ='');目标属性是'Text'(类型'String')“。虽然我之前做了一些绑定,但我无法弄清错误并花了2天时间:(。 控制代码:
Public Class PurchaseStockControl
Implements ComponentModel.INotifyPropertyChanged
Public Event PropertyChanged(sender As Object, e As ComponentModel.PropertyChangedEventArgs) Implements ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Sub NotifyPropertyChanged(ByVal PropertyName As String)
RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs(PropertyName))
End Sub
Property Namee As String
Get
Return "Some Value"
End Get
Set(value As String)
NotifyPropertyChanged("Namee")
End Set
End Property
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
带用户控制的主窗口:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:z="clr-namespace:WPFApp"
Title="MainWindow" Height="600" Width="800">
<Grid>
<z:PurchaseStockControl x:Name="Test"/>
</Grid>
</Window>
答案 0 :(得分:0)
您没有设置视图的DataContext
。在您的视图的构造函数中尝试执行
DataContext = this;
方法调用后 IntializeComponents()
。