如何解决WPF中的"绑定表达式路径错误"?

Bri*_*n J 3 wpf binding properties mvvm

我将一个可观察的模型对象集合绑定到数据网格.但是当我设置绑定到集合时,我得到了一个路径错误的peoprties.

在调试此问题时,我检查了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')
Run Code Online (Sandbox Code Playgroud)

有人能指出我正确的方向,以进一步调试吗?

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>
Run Code Online (Sandbox Code Playgroud)

视图模型包含一个名为Customers的类型为CustomerModel的Observable集合.这就是我将DataGrid ItemSource设置为的内容.(我已从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));
            }
        }



    }
}
Run Code Online (Sandbox Code Playgroud)

这些是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));
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是在后面的View代码中设置数据上下文的方式:

    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;

        }

    }          
Run Code Online (Sandbox Code Playgroud)

Rac*_*hel 8

这些绑定错误与您的DataGrid无关.

他们表明你有3个文本框的地方的名字fNameTbx,lNameTbx和emailTbx.DataGrid不会使用Name属性生成它的项目,因此它不是导致这些绑定错误的项目.

当试图读取绑定错误,最好用分号来打破他们和向后阅读,这表现在这里.

例如,

System.Windows.Data错误:40:BindingExpression路径错误:'object'''MainViewModel'(HashCode = 55615518)'上找不到'FirstName'属性.BindingExpression:路径=姓; DataItem ='MainViewModel'(HashCode = 55615518); target元素是'TextBox'(Name ='fNameTbx'); target属性是'Text'(类型'String')

也可以读作

  • target属性是'Text'(类型'String')
  • target元素是'TextBox'(Name ='fNameTbx');
  • DataItem ='MainViewModel'(HashCode = 55615518);
  • BindingExpression路径错误:'object'''MainViewModel'(HashCode = 55615518)'上找不到'FirstName'属性.BindingExpression:路径=姓;

在某处你有意思

<TextBox Name="fNameTbx" Text="{Binding FirstName}" />
Run Code Online (Sandbox Code Playgroud)

当DataContext这个文本框是类型MainViewModel.而且MainViewModel没有财产FirstName.

我建议在项目中搜索这些名称,或者使用像Snoop这样的工具在运行时调试数据绑定和DataContext问题.