2015-12-14 154 views
0

我很努力地弄清楚为什么当我将文本输入到文本框中并离开控件时,为什么没有触发propertychanged事件或lostfocus事件。TextBox不触发PropertyChanged事件

我正在构建一个通用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; 
     } 
    } 
+0

我不知道 - 是否缺少*双向*绑定?你想让你的TextBox在编辑后设置属性? – Romasz

+0

我以为TwoWay绑定是TextBoxes的默认设置? –

+0

它没有开火,因为你没有告诉它。您没有在XAML或C#中引​​用LostFocus。如果你在文本框中进出,没有任何改变,所以在你的属性设置器中没有任何东西被触发。 – CoderForHire

回答

2

的德发对于ULT结合模式UWPas MSDN says

默认是单向的。

将其设为双向

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