我正在将一个可观察的模型对象集合绑定到数据网格。但是当我设置集合的绑定时,我会得到一个到人员的路径错误。
在调试此问题时,我检查了CustomerModel中的公共属性在DataGrid绑定中的名称是否正确。而且返回给模型的集合不是空的。我还检查了View的代码隐藏中的数据上下文设置是否正确。
我认为这可能是一个错误,因为我在xaml中指定绑定路径的方式。。
对于每个字段,绑定错误的全部细节如下:
System.Windows.Data Error: 40 : BindingExpression path error: 'FirstName' property not found on 'object' ''MainViewModel' (HashCode=55615518)'. BindingExpression:Path=FirstName; DataItem='MainViewModel' (HashCode=55615518); target element is 'TextBox' (Name='fNameTbx'); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'LastName' property not found on 'object' ''MainViewModel' (HashCode=55615518)'. BindingExpression:Path=LastName; DataItem='MainViewModel' (HashCode=55615518); target element is 'TextBox' (Name='lNameTbx'); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Email' property not found on 'object' ''MainViewModel' (HashCode=55615518)'. BindingExpression:Path=Email; DataItem='MainViewModel' (HashCode=55615518); target element is 'TextBox' (Name='emailTbx'); target property is 'Text' (type 'String')
有人能为我指明正确的方向吗,以便进一步调试它?
DataGrid绑定路径和源设置如下:
<DataGrid Name="infogrid"
Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="1"
Grid.ColumnSpan="3"
AutoGenerateColumns="False"
ItemsSource="{Binding Customers}"
SelectedItem="{Binding SelectedCustomer}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Customers.Id}" Header="ID" />
<DataGridTextColumn Binding="{Binding Customers.FirstName}" Header="First Name" />
<DataGridTextColumn Binding="{Binding Customers.LastName}" Header="Last Name" />
<DataGridTextColumn Binding="{Binding Customers.Email}" Header="Email" />
</DataGrid.Columns>
</DataGrid>
视图模型包含一个类型为CustomerModel的Observable集合,称为Customers。这就是我将DataGridItemSource设置为的值。(为了可读性,我从VM中删除了其他代码)
namespace MongoDBApp.ViewModels
{
class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private ICustomerDataService _customerDataService;
public MainViewModel(ICustomerDataService customerDataService)
{
this._customerDataService = customerDataService;
QueryDataFromPersistence();
}
private ObservableCollection<CustomerModel> customers;
public ObservableCollection<CustomerModel> Customers
{
get
{
return customers;
}
set
{
customers = value;
RaisePropertyChanged("Customers");
}
}
private void QueryDataFromPersistence()
{
Customers = _customerDataService.GetAllCustomers().ToObservableCollection();
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
这些是CustomerModel中的字段,所以不确定为什么在绑定过程中找不到属性:
public class CustomerModel : INotifyPropertyChanged
{
private ObjectId id;
private string firstName;
private string lastName;
private string email;
[BsonElement]
ObservableCollection<CustomerModel> customers { get; set; }
/// <summary>
/// This attribute is used to map the Id property to the ObjectId in the collection
/// </summary>
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("firstName")]
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
RaisePropertyChanged("FirstName");
}
}
[BsonElement("lastName")]
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
RaisePropertyChanged("LastName");
}
}
[BsonElement("email")]
public string Email
{
get
{
return email;
}
set
{
email = value;
RaisePropertyChanged("Email");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这就是在视图的代码隐藏中设置数据上下文的方式:
public partial class MainView : Window
{
private MainViewModel ViewModel { get; set; }
private static ICustomerDataService customerDataService = new CustomerDataService(CustomerRepository.Instance);
public MainView()
{
InitializeComponent();
ViewModel = new MainViewModel(customerDataService);
this.DataContext = ViewModel;
}
}
这些绑定错误与DataGrid无关。
它们表示您有3个名为fNameTbx
、lNameTbx
和emailTbx
的文本框。DataGrid不会生成具有Name属性的项,因此它不是导致这些绑定错误的原因。
当试图读取绑定错误时,最好用分号将它们分解并向后读取,如这里所示。
例如,
System.Windows.Data错误:40:BindingExpression路径错误:在对象"MainViewModel"(HashCode=5551518)"上找不到"FirstName"属性。BindingExpression:Path=FirstName;DataItem="MainViewModel"(哈希代码=55615518);目标元素是"TextBox"(名称="NameTbx");目标属性是"文本"(类型为"字符串")
也可以读取为
- 目标属性为"Text"(类型为"String")
- 目标元素是"TextBox"(名称="NameTbx")
- DataItem="MainViewModel"(哈希代码=55615518)
- BindingExpression路径错误:在"object"MainViewModel"(HashCode=5551518)"上找不到"FirstName"属性。BindingExpression:Path=FirstName
这意味着你有
<TextBox Name="fNameTbx" Text="{Binding FirstName}" />
其中,此TextBox的DataContext
属于MainViewModel
类型。并且MainViewModel
不具有FirstName
的性质。
我建议您在项目中搜索这些名称,或者您可以使用Snoop之类的工具在运行时调试数据绑定和DataContext问题。
异常表示DataBinding引擎正在MainViewModel
上查找字段FirstName
、LastName
等,而不是CustomerModel
。
您不需要在列的各个绑定表达式中指定属性Customers
:
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Header="ID" />
<DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" />
<DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" />
<DataGridTextColumn Binding="{Binding Email}" Header="Email" />
</DataGrid.Columns>
当我在DataTemplate中有TextBlock文本绑定时,我也遇到了同样的问题,最终我不得不做:
Text={Binding DataContext.SuccessTxt}
以使其正常工作。尝试在属性前面添加"DataContext.",看看这是否有效。
public Window()
{
this.DataContext = this;
InitializeComponent();
}
public string Name {get;set;}
//xaml
<TextBlock Text="{Binding Name}"/>
InitializeComponent();
之前的this.DataContext = this;
(在InitializeComponent()
中加载xaml之前,DataContext需要可用)
属性Name
应为public
和{ get; }
(如果是私有的,则wpf无法访问)
我以前也遇到过同样的问题,使其正常工作的解决方案只是使用属性而不是类字段。Class字段的绑定似乎有问题。也许您应该在视图模型中使用属性而不是字段。
// This field not working
public SolidColorBrush BrushColor;
// But this property the binding worked
public SolidColorBrush BrushColor { get; set; }