我正在开发一个几乎是应用程序表单的WPF应用程序。它有多个文本字段。这是我使用MVVM模型的第一个应用程序,所以我确信我错过了一些东西。我有很多文本框,所以我展示的代码将集中在四个文本字段,第四个字段假设总计与其他三个字段一样。但是我的数据绑定没有执行。我无法弄清楚为什么当我在文本框中键入数字txbFamO时,不会执行FamilyO get语句。我没有正确绑定吗?申请人是否没有正确初始化?
这是我的XAML:
<UserControl x:Class="ApplicationForm.Views.ApplicationView"
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:local="clr-namespace:ApplicationForm"
xmlns:model="clr-namespace:ApplicationForm.Model"
xmlns:views="clr-namespace:ApplicationForm.Views"
xmlns:viewModel="clr-namespace:ApplicationForm.ViewModel"
mc:Ignorable="d"
Height="1100" Width="800" Background="#FFDAFDF2">
<Grid x:Name="grdAppForm">
<Grid x:Name="grdAppFormGrid" DataContext="{Binding Applicant}"
Height="881" Width="700" Margin="0,0,0,0">
<Label x:Name="lblFamilySize" Content="Family Size:" Height="28" Width="102" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="372,71,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16" />
<Label x:Name="lblFamO" Content="O:" Height="28" Width="27" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="471,71,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
<TextBox x:Name="txbFamO" Text="{Binding FamilyO, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="22" Width="20" HorizontalAlignment="Left"
VerticalAlignment="Top" Margin="496,71,0,0" FontFamily="Arial" FontSize="16"
FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
<Label x:Name="lblFamA" Content="A:" Height="28" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="516,71,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
<TextBox x:Name="tbxFamA" Text="{Binding FamilyA, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Height="22" Width="20" HorizontalAlignment="Left"
VerticalAlignment="Top" Margin="541,71,0,0" FontFamily="Arial" FontSize="16"
FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
<Label x:Name="lblFamC" Content="C:" Height="28" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="562,71,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
<TextBox x:Name="txtFamC" Text="{Binding FamilyC, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Height="22" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="587,71,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
<Label x:Name="lblEqual" Content="=" Height="28" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="611,71,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
<TextBox x:Name="tbxFamTot" Text="{Binding FamilyTotal,
Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" Height="22" Width="30"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="634,71,0,0" FontFamily="Arial"
FontWeight="Bold" FontSize="16" BorderBrush="Black" BorderThickness="0,0,0,3" IsTabStop="False"
VerticalContentAlignment="Bottom"/>
</Grid>
</Grid>
我的型号代码:
namespace ApplicationForm.Model
{
public class ApplicationModel
{
}
public class Applicant : INotifyPropertyChanged
{
private string familyO;
private string familyA;
private string familyC;
public string FamilyO
{
get { return familyO; }
set
{
if (familyO != value)
{
familyO = value;
RaisePropertyChanged("FamilyO");
RaisePropertyChanged("FamilyTotal");
}
}
}
public string FamilyA
{
get { return familyA; }
set
{
if (familyA != value)
{
familyA = value;
RaisePropertyChanged("FamilyA");
RaisePropertyChanged("FamilyTotal");
}
}
}
public string FamilyC
{
get { return familyC; }
set
{
if (familyC != value)
{
familyC = value;
RaisePropertyChanged("FamilyC");
RaisePropertyChanged("FamilyTotal");
}
}
}
public string FamilyTotal
{
get
{
return (Convert.ToInt16(familyO) + Convert.ToInt16(familyA) + Convert.ToInt16(familyC)).ToString();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
我的ViewModel代码:
namespace ApplicationForm.ViewModel
{
class ApplicationViewModel : INotifyPropertyChanged
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
static string[] Scopes = { SheetsService.Scope.Spreadsheets };
static string ApplicationName = "NB Food Pantry Application";
public RelayCommand LoadApplicantCommand
{
get;
set;
}
public RelayCommand ClearDataCommand
{
get;
set;
}
public RelayCommand PrintApplicantCommand
{
get;
set;
}
public RelayCommand CloseCommand
{
get;
set;
}
public Applicant applicants;
public ApplicationViewModel()
{
applicants = new Applicant();
PrintApplicantCommand = new RelayCommand(PrintApplicant);
ClearDataCommand = new RelayCommand(ClearApplicantData);
CloseCommand = new RelayCommand(CloseApp);
}
public ObservableCollection<Applicant> Applicants
{
get;
set;
}
}
}