从类库中绑定Silverlight 3中没有发生绑定

时间:2011-04-17 13:22:37

标签: silverlight-3.0

----------我正在调用WCF服务的类库文件-------------

public class AllEmployeeViewModel
    {

       // public ObservableCollection<Employee> Employees { get; set; }
        public List<Employee> Employees { get; set; }
public void GetAllEmployees()
        {

            Proxy.EmployeeListCompleted += new EventHandler<EmployeeListCompletedEventArgs>(client_EmployeeListCompleted);
            Proxy.EmployeeListAsync();

        }

void client_EmployeeListCompleted(object sender, EmployeeListCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    Employees = e.Result;
                }
            }
            catch (Exception)
            {
                throw;
            }

        }
    }

--------- MainPage.xaml中-----------------

<UserControl.Resources>
        <scr:AllEmployeeViewModel  x:Key="empKey"  />
    </UserControl.Resources>

    <Grid x:Name="MainGrid" Background="White" Width="400"
 Height="407" DataContext="{Binding Source={StaticResource empKey}}">
        <Grid x:Name="grdAllEmp" DataContext="{Binding Path=Employees}">

            <data:DataGrid AutoGenerateColumns ="True" Height="274"
    HorizontalAlignment="Left" Margin="8,8,0,0"
     Name="dgEmployee" VerticalAlignment="Top" Width="385"
    ItemsSource="{Binding}">
               <data:DataGrid.Columns>

                    <data:DataGridTextColumn Header="FName" Binding="{Binding FName}">
                    </data:DataGridTextColumn>
                    <data:DataGridTextColumn Header="LName" Binding="{Binding LName}">
                    </data:DataGridTextColumn>
                </data:DataGrid.Columns>
            </data:DataGrid>
</Grid>
    </Grid>

这里我将类文件中的数据作为datacontext发送到mainpage.xaml但是没有发生绑定,在我的类文件AllEmployeeViewMode中我可以点击wcf并在这里获取数据

if(e.Error == null)  {      员工= e.Result;   }

任何帮助如何解决这个问题都很棒

感谢

王子

回答我是如何解决的

public class AllEmployeeViewModel
    {

        public ObservableCollection<Employee> Employees { get; set; }
        WCF.EMPServiceClient Proxy;


        public AllEmployeeViewModel()
        {
            try
            {
                Employees = new ObservableCollection<Employee>();
                Proxy = new WCF.EMPServiceClient();

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public ICommand GetEmployees
        {
            get
            {
                return new GetAllEmployeeCommand(this);
            }
        }

        public void GetAllEmployees()
        {

            Proxy.EmployeeListCompleted += new EventHandler<EmployeeListCompletedEventArgs>(client_EmployeeListCompleted);
            Proxy.EmployeeListAsync();

        }


        void client_EmployeeListCompleted(object sender, EmployeeListCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    var emp = e.Result;
                    foreach (var item in emp)
                        Employees.Add(item);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }


    }

然后这些值在我的xaml中被绑定

1 个答案:

答案 0 :(得分:0)

您尚未为Employees属性实现NotifyPropertyChanged。我可以看到上面注释的属性,这是一个ObsevableCollection。如果您使用它,绑定应该没有任何问题。 有关详细信息,请参阅此link