技术开发 频道

实际操作WPF:将数据绑定到对象集合

  10. 到此为止第二个功能页面就完全定义好了。我们把它们和第一个页面上的返回事件处理函数和Next按钮click处理函数绑定到一起。在第一个页面的代码文件ContactDetailPage1.xaml.cs里添加如下代码:

        //
        
// On clicking the next button on PF1, navigate to PF2
        
//
        private void OnNextClick(object sender, RoutedEventArgs e)
        {

            NavigationService ns
=
                         NavigationService.GetNavigationService(
this);
            ContactDetailPage2 pageFunction
= new ContactDetailPage2();
            pageFunction.Return
+= new
                         ReturnEventHandler
<Object>(pageFunction2_Return);
            ns.Navigate(pageFunction);
        }

        
//
        
// When the second PF returns, get the Contact object it
        
// sent back and add the first PF’s data to it
        
// Then return to the UILessPageFunction that called this
        
// ContactDetailPage1 PF.
        
//
        void pageFunction2_Return(object sender, ReturnEventArgs<object> e)
        {
            Contact c
= e.Result as Contact;
            c.FirstName
= this.txtFirstName.Text;
            c.LastName
= this.txtLastName.Text;
            c.HomePage
= new Uri(this.txtHomePage.Text);
            c.EmailAddress
= this.txtEmailAddress.Text;
            OnReturn(
new ReturnEventArgs<object>(c));
        }

  11. 您的应用程序就差两步就完成了!当联系人列表当中的某一项被选定的时候,我们要把联系人详细信息显示在右边的面板。下面我么就来写这一段代码。我们不是曾经在MainWindow.xaml.cs里面创建了一个空的ListItemSelected方法吗?现在我们把它实现吧:

       //
        
// Triggered when an item in the Contacts list is selected
        
//
        void ListItemSelected(object sender, SelectionChangedEventArgs args)
        {
            
// show first page function on the right hand side frame
            ContactDetailPage1 pageFunction =
                  
new ContactDetailPage1(false, allContacts.SelectedIndex);
            pageFunction.Return
+= new
                   System.Windows.Navigation.ReturnEventHandler
<object>
                            (pageFunction0_Return);
            
this.Frame_RightPane.Navigate(pageFunction);
            
            
//update status bar
            ContactList cl =
                  Application.Current.Properties[
"ContactList"]
                              
as ContactList;
            Contact c
= cl[allContacts.SelectedIndex];
            
this.tb.Text = c.FirstName + " " + c.LastName;
        }

  因为ContactDetailPage1对话框是以只读模式显示的,它不应该返回任何值。我们就在MainWindow.xaml.cs里创建一个空的pageFunction0_Return,好让程序能顺利编译。

        //
        
// This is the ContactDetailPage1 page function's return handler.
        
// Merely a placeholder since the page is displayed in read mode and
        
// doesn't return anything.
        
//
        void pageFunction0_Return(object sender,
                         System.Windows.Navigation.ReturnEventArgs
<object> e)
        {
        }

 

0
相关文章