技术开发 频道

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

  8. 像我们在第一个page function里做过的一样,我们创建第二个功能页ContactDetailPage2,设定类型参数为Object。这个页面由两个TextBox组成,一个显示家庭地址,另一个显示公司地址。另外还添加一个Cancel和一个Finish按钮。

<PageFunction x:Class="AddressBook.ContactDetailPage2"
              x:TypeArguments
="sys:Object"
              xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:sys
="clr-namespace:System;assembly=mscorlib"
              Title
="ContactDetailPage2" >
  
    
<Grid
      
Name="RootGrid"
      VerticalAlignment
="Center"
      HorizontalAlignment
="Center" >
      
      
<Grid.ColumnDefinitions>
        
<ColumnDefinition Width="200"/>
        
<ColumnDefinition Width="200"/>
      
</Grid.ColumnDefinitions>
      
<Grid.RowDefinitions>
        
<RowDefinition Height="30"/>
        
<RowDefinition Height="110"/>
        
<RowDefinition Height="110"/>
        
<RowDefinition Height="60"/>
      
</Grid.RowDefinitions>

        
<!-- Labels -->
        
<TextBlock Width="100" Height="30"
            Grid.Column
="0" Grid.Row="1" > Business Address</TextBlock>
        
<TextBlock Width="100" Height="30"
            Grid.Column
="0" Grid.Row="2" > Home Address</TextBlock>

        
<!-- Input fields -->
        
<TextBox Name="txtHomeAddress" Width="200" Height="100"
                 Grid.Column
="1" Grid.Row="1" TextWrapping="Wrap"
                 Text
="{Binding Path=HomeAddress, Mode=TwoWay}"/>
        
<TextBox Name="txtBusinessAddress" Width="200" Height="100"
                 Grid.Column
="1" Grid.Row="2" TextWrapping="Wrap"
                 Text
="{Binding Path=BusinessAddress, Mode=TwoWay}"/>

        
<DockPanel Name="ButtonPanel1" Grid.Column="0" Grid.Row="3" >
                
<Button Name="btnBack" Width="100" Height="30"
                        Click
="OnBackClick">Back</Button>
                
<Button Name="btnCancel" Width="100" Height="30"
                        Click
="OnCancelClick">Cancel</Button>
        
</DockPanel>
        
<DockPanel Name="ButtonPanel2" Grid.Column="1" Grid.Row="3">
            
<Button Name="btnFinish" Width="100" Height="30"
                    Click
="OnFinishClick">Finish</Button>
        
</DockPanel>

    
</Grid>
</PageFunction>

  9. ContactDetailPage2功能页的代码非常直观。再次强调要把类型参数改为Object。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AddressBook
{
    
/// <summary>
    
/// Interaction logic for ContactDetailPage2.xaml
    
/// </summary>
    public partial class ContactDetailPage2 : PageFunction<Object>
    {
        
public ContactDetailPage2()
        {
            InitializeComponent();
        }


        
/// <summary>
        
/// Create a ContactDetailPage2 with specified edit mode value.
        
/// Since we won’t be displaying this page on the right hand side
        
/// Frame, this constructor does not accept and use the index
        
/// of selected element in the ContactList.
        
/// </summary>
        public ContactDetailPage2(bool editmode)
        {
            
            InitializeComponent();

            
if (editmode == false)
            {
                ContactList cl
=
                           Application.Current.Properties[
"ContactList"]
                                      
as ContactList;
                Contact c
= cl[0];
                
this.RootGrid.DataContext = c;

                
this.RootGrid.IsEnabled = false;
                
this.ButtonPanel1.Visibility = Visibility.Hidden;
                
this.ButtonPanel2.Visibility = Visibility.Hidden;
            }
        }

        
//
        
// Navigate back if the Back button is clicked
        
//
        private void OnBackClick(object sender, RoutedEventArgs e)
        {
            NavigationService ns
=
                         NavigationService.GetNavigationService(
this);
            ns.GoBack();

        }


        
//
        
// Close window if Cancel button is clicked
        
//
        private void OnCancelClick(object sender, RoutedEventArgs e)
        {
            ((NavigationWindow)(
this.Parent)).Close();

        }

        
//
        
// When the Finish button is clicked, hydrate the Contact
        
// object with page data and return to our caller, which is
        
// ContactDetailPage1 page function.
        
//
        private void OnFinishClick(object sender, RoutedEventArgs e)
        {
            Contact c
= new Contact();
            c.HomeAddress
= this.txtHomeAddress.Text;
            c.BusinessAddress
= this.txtBusinessAddress.Text;
            OnReturn(
new ReturnEventArgs<object>(c));
            
// close the wizard
            Window w = Application.Current.Properties["AddContactWizard"]
                            
as Window;
            w.Close();

        }
    }
}
0
相关文章