似乎无法加载datagrid wpf你能帮忙吗?

时间:2010-07-03 09:46:33

标签: wpf

由于某些原因我似乎无法填充数据网格,我收到以下错误:

System.Windows.Data错误:40:BindingExpression路径错误:'客户'属性未找到'对象'''ObservableCollection`1'(HashCode = 11497055)

你能发现我做错了吗?

代码如下:

    ViewModel
    =============
        public class CustomerVM : ViewModelBase
        {
            private ObservableCollection<Customer> _customers;          
            public ObservableCollection<Customer> Customers
            {
                get { return _customers; }
                set
                {
                    _customers = value;
                    OnPropertyChanged("Customers");
                }
            }   

            private DelegateCommand _getCustomersCommand;
            public ICommand GetCustomersCommand
            {
                get
                {
                    return _getCustomersCommand ?? (_getCustomersCommand = new DelegateCommand(x => GetCustomers(), x => CanGetCustomer));
                }
            }
            private static bool CanGetCustomer
            {
                get { return true; }
            }
            private void GetCustomers()
            {
                Customers = new ObservableCollection<Customer>
                           {
                               new Customer{Id=1,Name ="Jo",Surname = "Bloggs",IsMember = true},
                               new Customer{Id=2,Name ="Mark", Surname = "Cole",IsMember = false},
                               new Customer{Id=3,Name ="Robert ", Surname = "Smith",IsMember = true},
                               new Customer{Id=4,Name ="Vincent", Surname = "Varc",IsMember = false},
                           };
            }
        }    


          Customer Wpf Window
          ====================
          <Window x:Class="Sample.CustomerDataGridSample"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
            Title="CustomerDataGridSample" Height="300" Width="300" Loaded="OnWindowsLoaded" WindowStartupLocation="CenterScreen">

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBlock Text="Customers"
                       Grid.ColumnSpan="2"
                       FontWeight="Bold"
                       FontSize="12"/>
            <toolkit:DataGrid x:Name="dg" Grid.Row="1"
                               ItemsSource="{Binding Customers}"                            
                               CanUserAddRows="False" 
                               CanUserDeleteRows="False"
                               CanUserResizeRows="False" 
                               CanUserSortColumns="False"
                               AutoGenerateColumns="False"
                               RowHeaderWidth="17" RowHeight="25">
                <toolkit:DataGrid.Columns>
                    <toolkit:DataGridTextColumn   Header="Name"
                                              Binding="{Binding Path=Name}" />
                    <toolkit:DataGridTextColumn   Header="Surname"
                                              Binding="{Binding Path=Surname}" />
                    <toolkit:DataGridTextColumn   Header="Is Member"
                                              Binding="{Binding Path=IsMember}" />
                </toolkit:DataGrid.Columns>
            </toolkit:DataGrid>
        </Grid>
    </Window>

     public partial class CustomerDataGridSample:Window
    {
        public CustomerDataGridSample()
        {
            InitializeComponent();
        }
        private void OnWindowsLoaded(object sender, RoutedEventArgs e)
        {
            var customerVm = new CustomerVM();
            customerVm.GetCustomersCommand.Execute(null);
            DataContext = customerVm.Customers;
        }
    }

1 个答案:

答案 0 :(得分:1)

单独的错误消息就会将其丢弃 - 您的绑定具有错误的上下文。

您在加载的方法中将窗口的DataContext设置为customerVm.Customers。这意味着您的绑定已经从Customers属性“开始”,因此网格上的绑定实际上是在尝试查找customerVm.Customers.Customers。这就是您收到错误消息的原因:ObservableCollection没有Customers属性。

您可以更改它,以便代码中设置的DataContext只是customerVm,或者在DataGrid上使用ItemsSource={Binding}(然后只需选择当前DataContext )。