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

时间:2015-11-26 10:55:35

标签: wpf xaml mvvm data-binding datagrid

我已经设置了一个DataGrid,它绑定到一个Observable CustomerModel对象集合。我还为该模型中的每个字段设置了属性,并为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();
        } 
    }
}

1 个答案:

答案 0 :(得分:2)

错误在于:

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

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