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

2024-04-19

我将模型对象的可观察集合绑定到数据网格。但是当我将绑定设置到集合时,我收到了指向人员的路径错误。

在调试此问题时,我检查了 CustomerModel 中的公共属性在 DataGrid 绑定中是否正确命名。而且返回到模型的集合不为空。我还检查了视图后面的代码中的数据上下文是否设置正确。

我认为由于我在 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。这就是我将 DataGrid ItemSource 设置为的内容。 (为了便于阅读,我从虚拟机中删除了其他代码)

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, and emailTbx。 DataGrid 不会生成具有 Name 属性的项目,因此它不是导致这些绑定错误的原因。

当尝试读取绑定错误时,最好用分号将它们分开并向后读取,如下所示here https://stackoverflow.com/a/14523290/302677.

例如,

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“MainViewModel”(HashCode=55615518) 上找不到“FirstName”属性。 BindingExpression:Path=名字; DataItem='MainViewModel'(哈希码=55615518);目标元素是“TextBox”(名称=“fNameTbx”);目标属性是“文本”(类型“字符串”)

也可以读作

  • 目标属性是“文本”(类型“字符串”)
  • 目标元素是“TextBox”(名称=“fNameTbx”);
  • DataItem='MainViewModel'(哈希码=55615518);
  • BindingExpression 路径错误:在“对象”“MainViewModel”(HashCode=55615518) 上找不到“FirstName”属性。 BindingExpression:Path=名字;

意思是你在某个地方

<TextBox Name="fNameTbx" Text="{Binding FirstName}" />

哪里的DataContext此文本框的类型为MainViewModel. And MainViewModel没有以下属性FirstName.

我建议在您的项目中搜索这些名称,或者您可以使用类似的工具Snoop https://snoopwpf.codeplex.com/在运行时调试数据绑定和 DataContext 问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何解决 WPF 中的“绑定表达式路径错误”? 的相关文章

随机推荐