【问题标题】:Windows Phone 7 MVVM databinding formWindows Phone 7 MVVM 数据绑定表单
【发布时间】:2012-09-11 09:07:53
【问题描述】:

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

【问题讨论】:

  • 您没有显示公开Forename 属性的代码。如果您将代码精简到出现问题的最小示例,这将非常有帮助。

标签: windows-phone-7 xaml mvvm mvvm-light


【解决方案1】:

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

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

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

【讨论】:

  • 我的 LayoutRoot 绑定到申请人有必要吗?
  • 这是一种向 xaml 解释在哪里搜索值的方法 :)
猜你喜欢
  • 1970-01-01
  • 2012-05-30
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多