2012-09-19 2 views
0

저는 Windows Phone Development를 처음 사용하며 현재 간단한 양식을 가지고 있습니다. MVVM 패턴과 MvvmLight를 사용하여 명령을 실행하고 있습니다. "신청자"엔티티는 항상 비어 있으며 데이터 바인딩이 작동하지 않는 2 가지 방법처럼 보입니다. 여기 내 코드가 있습니다, 나는 누군가가 올바른 방향으로 나를 가리킬 수 있기를 바랍니다.Windows Phone 7 MVVM 데이터 바인딩 양식

보기

<Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{Binding Applicant}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
     <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> 
     <TextBlock x:Name="PageTitle" Text="create account" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
    </StackPanel> 

    <!--ContentPanel - place additional content here--> 
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <StackPanel> 
      <StackPanel> 
       <TextBlock FontSize="15" TextWrapping="Wrap" Margin="0,0,0,10" Text="Please register to create your applicant account. No information will be passed to third parties for any reason."></TextBlock> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock VerticalAlignment="Center" Text="Forename" Width="100"></TextBlock> 
       <TextBox Width="350" BorderBrush="Red" DataContext="{Binding Forename, Mode=TwoWay}"></TextBox> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock VerticalAlignment="Center" Text="Surname" Width="100"></TextBlock> 
       <TextBox Width="350" x:Name="Surname" BorderBrush="Red" DataContext="{Binding Surname, Mode=TwoWay}"></TextBox> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock VerticalAlignment="Center" Text="Email" Width="100"></TextBlock> 
       <TextBox Width="350" x:Name="EmailAddress" BorderBrush="Red" DataContext="{Binding EmailAddress, Mode=TwoWay}"></TextBox> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock VerticalAlignment="Center" Text="Password" Width="100"></TextBlock> 
       <PasswordBox Width="350" x:Name="PassPhrase" BorderBrush="Red" DataContext="{Binding PassPhrase, Mode=TwoWay}"></PasswordBox> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock VerticalAlignment="Center" Text="City" Width="100"></TextBlock> 
       <TextBox Width="350" x:Name="City" DataContext="{Binding City, Mode=TwoWay}"></TextBox> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock VerticalAlignment="Center" Text="State" Width="100"></TextBlock> 
       <TextBox Width="350" x:Name="County" DataContext="{Binding County, Mode=TwoWay}"></TextBox> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock VerticalAlignment="Center" Text="Country" Width="100"></TextBlock> 
       <TextBox Width="350" x:Name="Country" DataContext="{Binding Country, Mode=TwoWay}"></TextBox> 
      </StackPanel> 
      <StackPanel> 
       <Button Name="btnContact" 
         Content="Register" 
         DataContext="{Binding ElementName=this, Path=DataContext}" 
         Command="{Binding SaveApplicantCommand}" 
         Width="300"/> 
      </StackPanel> 
     </StackPanel> 
    </Grid> 
</Grid> 

보기 코드

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 

     ApplicantViewModel vm = new ApplicantViewModel(); 
     DataContext = vm; 
    } 

보기 모델

public class ApplicantViewModel : ViewModelBase 
{ 
    public ApplicantViewModel() 
    { 
     SaveApplicantCommand = new RelayCommand(SaveApplicant); 
     Applicant = new Applicant(); 
    } 

    public ICommand SaveApplicantCommand {get; set;} 

    void SaveApplicant() 
    { 
     if (string.IsNullOrEmpty(Applicant.Forename)) 
     { 
      MessageBox.Show("Please enter a forename", "Oops...", MessageBoxButton.OK); 
      return; 
     } 

     db.AddObject("Applicants", Applicant); 
     db.BeginSaveChanges(OnChangesSaved, db); 
    } 

    void OnChangesSaved(IAsyncResult result) 
    { 

    } 

    private Applicant _applicant; 
    public Applicant Applicant 
    { 
     get 
     { 
      return _applicant; 
     } 
     set 
     { 
      if (_applicant != value) 
      { 
       _applicant = value; 
       RaisePropertyChanged("Applicant"); 
      } 
     } 
    } 
} 
,536을 (뷰 모델에서 INotifyPropertyChanged를 구현 ViewModelBase에서 상속) 뒤에

Forename 텍스트 상자에 입력되는 내용은 모두 Forename이 null이므로 항상 오류 메시지가 표시됩니다.

** ** * UPDATE * ** *** ** * 여기

모델의 특성이다 뒤따라야

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")] 
    public string Forename 
    { 
     get 
     { 
      return this._Forename; 
     } 
     set 
     { 
      this.OnForenameChanging(value); 
      this._Forename = value; 
      this.OnForenameChanged(); 
      this.OnPropertyChanged("Forename"); 
     } 
    } 
+1

'Forename' 속성을 노출하는 코드를 표시하지 않았습니다. 문제를 보여주는 가장 작은 예제로 코드를 제거하면 정말 도움이 될 것입니다. – ColinE

답변

2

ApplicantViewModel에는 Forename 속성이 없습니다. 신청자 만이 있습니다. 따라서 다음을 수행하십시오.

<TextBox Width="350" BorderBrush="Red" 
     DataContext="{Binding Applicant.Forename, Mode=TwoWay}"/> 

다른 텍스트 상자와 동일하게하십시오.

+0

내 LayoutRoot가 신청자에게 연결되어 있으므로 필요한가요? – Paul

+0

이것은 xaml에게 값을 검색 할 위치를 설명하는 방법입니다. :) –