如何解决网格视图SelectedItem属性上的绑定路径错误



我已经设置了一个DataGrid,它绑定到CustomerModel对象的Observable集合。此外,我还为该模型中的每个字段设置了属性,并在VM中设置了MainViewModel类型的SelectedCustomer属性。

但是,当我从DataGrid中选择其中一行来填充下面的文本框时,我在字段属性、FirstName等上得到了一个路径错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'SelectedCustomer' property not found on 'object' ''DataGrid' (Name='customersgrid')'. BindingExpression:Path=SelectedCustomer.FirstName; DataItem='DataGrid' (Name='customersgrid'); target element is 'TextBox' (Name='fNameTbx'); target property is 'Text' (type 'String')
System.Windows.Data Error: 23 : Cannot convert 'MongoDBApp.Models.CustomerModel' from type 'CustomerModel' to type 'MongoDBApp.ViewModels.MainViewModel' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: TypeConverter cannot convert from MongoDBApp.Models.CustomerModel.

为了调试这个问题,我检查了View的数据上下文,它被设置为VM:

private MainViewModel ViewModel { get; set; }
private static ICustomerDataService customerDataService = new CustomerDataService(CustomerRepository.Instance);
public MainView()
{
    InitializeComponent();
    ViewModel = new MainViewModel(customerDataService); 
    this.DataContext = ViewModel;
}

我还检查了公共属性名称是否与UI上的绑定名称匹配,它们确实匹配。我知道第二个错误暗示它无法在CustomerModel类型的DataGrid绑定源和MainViewModel类型的SelectedItem属性之间转换。

有人知道我如何进一步调试这个吗?

UI XAML及其绑定路径的示例:

<Grid>
    <DataGrid x:Name="customersgrid" Grid.RowSpan="3"  Grid.Column="1" Grid.ColumnSpan="3" AutoGenerateColumns="False"
              ItemsSource="{Binding Customers}" SelectedItem="{Binding SelectedCustomer}">
        <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>
    </DataGrid>
    <Label Grid.Row="4" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" Content="First Name:" />
    <TextBox x:Name="fNameTbx" Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="2" Width="120"  Height="23"
             HorizontalAlignment="Left"  VerticalAlignment="Top" TextWrapping="Wrap"
             Text="{Binding SelectedCustomer.FirstName, ElementName=customersgrid}" /> 
</Grid>

以及MainViewModel的简短版本:

namespace MongoDBApp.ViewModels
{
    class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ICustomerDataService _customerDataService;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public MainViewModel(ICustomerDataService customerDataService) 
        {
            this._customerDataService = customerDataService;
            QueryDataFromPersistence();
        }
#region Properties
        private MainViewModel selectedCustomer;
        public MainViewModel SelectedCustomer
        {
            get { return selectedCustomer; }
            set
            {
                selectedCustomer = value;
                RaisePropertyChanged("SelectedCustomer");
            }
        }
        private ObservableCollection<CustomerModel> customers;
        public ObservableCollection<CustomerModel> Customers
        {
            get { return customers; }
            set
            {
                customers = value;
                RaisePropertyChanged("Customers");
            }
        }
        private string firstName;
        public string FirstName
        {
            get { return firstName; }
            set
            {
                firstName = value;
                RaisePropertyChanged("FirstName");
            }
        }
#endregion
        private void QueryDataFromPersistence()
        {
            Customers =  _customerDataService.GetAllCustomers().ToObservableCollection();
        } 
    }
}

错误在这里:

private MainViewModel selectedCustomer;
    public MainViewModel SelectedCustomer
    {
        get
        {
            return selectedCustomer;
        }
        set
        {
            selectedCustomer = value;
            RaisePropertyChanged("SelectedCustomer");
        }
    }

selectedCustomer的类型更改为CustomerModel,而不是MainViewModel

最新更新