0

我是 Windows Phone 开发的新手,目前有一个简单的表单。我正在使用 MVVM 模式和 MvvmLight 来运行命令。“申请人”实体始终为空,看起来 2 方式数据绑定不起作用。这是我的代码,我希望有人能指出我正确的方向。

看法

   <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;
    }

视图模型(视图模型继承自实现 INotifyPropertyChanged 的​​ ViewModelBase)

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");
            }
        }
    }
}

无论在 Forename 文本框中输入什么,我总是收到错误消息,因为 Forename 为空。

** * **更新* ** * ** * **

这是模型中的 Forename 属性

[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");
        }
    }
4

1 回答 1

2

ApplicationViewModel 没有 Forename 属性。只有申请人有。因此,请执行以下操作:

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

对其他文本框执行相同操作。

于 2012-09-19T12:22:47.623 回答