TextBox不会触发PropertyChanged事件

时间:2015-12-14 17:39:25

标签: xaml winrt-xaml win-universal-app

我正在努力弄清楚当我将文本输入文本框并离开控件时,为什么没有触发propertychanged事件或lostfocus事件。

我正在构建通用Windows平台应用程序。

我知道我正在做一些迟钝的事情,但我只是看不到它。

有什么建议吗?

XAML:

<Page
    x:Class="xxx.Client.FormPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:viewModels="using:xxx.xxx.ViewModels"
    Background="LightBlue"
    mc:Ignorable="d">

    <Page.Resources>
        <Style x:Key="TextBoxStyle" TargetType="TextBox">
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="TextAlignment" Value="Center" />
        </Style>
    </Page.Resources>

    <Page.DataContext>
        <viewModels:FormViewModel />
    </Page.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <TextBox Grid.Row="0" Text="{Binding InstructorNameInput}" Style="{StaticResource TextBoxStyle}" 
                 PlaceholderText="Instructor Name" />

        <TextBox Grid.Row="1" Text="{Binding InstructorIdInput}" Style="{StaticResource TextBoxStyle}"
                 PlaceholderText="Instructor Id" />

        <TextBox Grid.Row="2" Text="{Binding CourseInput}" Style="{StaticResource TextBoxStyle}"
                 PlaceholderText="Course" />

        <Button Grid.Row="4" Content="Accept" Command="{Binding Submit}" 
                HorizontalAlignment="Stretch"
                Foreground="Black" />
    </Grid>
</Page>

视图模型:

public partial class FormViewModel : ViewModelBase
    {
        public FormViewModel()
        {
            InstructorName = new RequiredField("Instructor's Name:");
            InstructorId = new RequiredField("Instructor's Employee#:");
            Course = new RequiredField("Course:");

            var courses = CourseFactory.Produce();
            var administration = new Administration(courses, null);
            Submit = new DelegateCommand(_=> OnSubmit(), CanSubmit);
        }

        string _instructorNameInput = null;
        public string InstructorNameInput
        {
            get { return _instructorNameInput; }
            set
            {
                if (_instructorNameInput != value)
                {
                    _instructorNameInput = value;
                    OnNotifyPropertyChanged();
                }
            }
        }

        string instructorIdInput = null;
        public string InstructorIdInput
        {
            get { return instructorIdInput; }
            set
            {
                if (instructorIdInput != value)
                {
                    instructorIdInput = value;
                    OnNotifyPropertyChanged();
                }
            }
        }

        string courseInput = null;
        public string CourseInput
        {
            get { return courseInput; }
            set
            {
                if (courseInput != value)
                {
                    courseInput = value;
                    OnNotifyPropertyChanged();
                }
            }
        }

        public RequiredField InstructorName { get; }
        public RequiredField InstructorId { get; }
        public RequiredField Course { get; }

        public ObservableCollection<RequiredField> RequiredFields { get; } = new ObservableCollection<RequiredField>();
    }

ViewModel.internal:

public partial class FormViewModel
    {
        static void OnSubmit()
        {
            var adminsistration = new Administration();
        }

        bool CanSubmit(object obj) => !GetUnsatisfied().Any();

        IEnumerable<RequiredField> GetUnsatisfied()
        {
            if (string.IsNullOrEmpty(InstructorIdInput))
            {
                RequiredFields.Add(InstructorId);
            }
            else
            {
                var result = 0;
                var isNumeric = int.TryParse(InstructorIdInput, out result);

                if (!isNumeric)
                {
                    RequiredFields.Add(new RequiredField(InstructorId.About, "Instructor ID must be numeric"));
                }
            }

            if (string.IsNullOrEmpty(InstructorNameInput))
            {
                RequiredFields.Add(InstructorName);
            }

            if (string.IsNullOrEmpty(CourseInput))
            {
                RequiredFields.Add(Course);
            }

            return RequiredFields;
        }
    }

1 个答案:

答案 0 :(得分:2)

The default binding mode for UWP as MSDN says:

The default is OneWay.

Set it to TwoWay:

<TextBox Grid.Row="0" Text="{Binding InstructorNameInput, Mode=TwoWay}" .../>