Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使数据绑定与null类一起工作?_C#_Wpf_Mvvm_Data Binding - Fatal编程技术网

C# 如何使数据绑定与null类一起工作?

C# 如何使数据绑定与null类一起工作?,c#,wpf,mvvm,data-binding,C#,Wpf,Mvvm,Data Binding,我正在开发一个WPF应用程序,它基本上是一个应用程序表单。它有多个文本字段。这是我使用MVVM模型做的第一个应用程序,所以我确信我遗漏了一些东西。我有很多文本框,所以我显示的代码将集中在四个文本字段上,第四个字段的总数将随着其他三个字段的变化而变化。但是,我的数据绑定没有执行。我不明白为什么在文本框txbFamO中键入一个数字时,FamilyO get语句没有执行。我装订不正确吗?我没有正确初始化类申请人吗 这是我的XAML: <UserControl x:Class="Applicati

我正在开发一个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>
}

我的ViewModel代码:

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));
        }
    }
}
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;
    }
}
namespace ApplicationForm.ViewModel
{
类ApplicationViewModel:INotifyPropertyChanged
{
//如果修改这些作用域,请删除以前保存的凭据
//网址~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
静态字符串[]范围={SheetsService.Scope.Spreadsheets};
静态字符串ApplicationName=“NB食品储藏室应用程序”;
public RelayCommand loadapplication命令
{
得到;
设置
}
公共中继命令ClearDataCommand
{
得到;
设置
}
public RelayCommand printplicantCommand
{
得到;
设置
}
公共中继命令CloseCommand
{
得到;
设置
}
公共申请人;
公共应用程序视图模型()
{
申请人=新申请人();
PrintAppliantCommand=新的中继命令(PrintAppliant);
ClearDataCommand=新的RelayCommand(ClearAppliantData);
CloseCommand=新的RelayCommand(CloseApp);
}
公开收集申请人
{
得到;
设置
}
}

}我已经解决了我的问题。我没有创建申请者实例并初始化元素。

“我显示的代码将集中在四个文本字段上”——您显示的代码应该是最小的。当然,它不需要四个文本框,加上所有其他的东西,只是为了证明它是你有什么问题。请阅读。另请参见,尤其是该页底部链接的文章。也就是说,您似乎完全没有在
ApplicationViewModel
类中实现
INotifyPropertyChanged
。我在任何地方都看不到一个申请者。您是否查看了调试输出以了解存在哪些绑定错误?以及“空类”到底意味着什么?我是如何未能实现INotifyPropertyChanged的?我的ApplicationViewModel在其定义中更改了InotifyProperty,在我的setters中更改了RaiseProperty。在我的XAML中,我将datacontext设置为我的申请者类,并在相应文本框的类字段上设置绑定。当我单步执行调试器时,不会调用setter代码。我在某处断开了连接,不知道在哪里。我假设我没有在启动时正确创建申请人的实例。我不确定如何创建绑定可以使用的应用程序类,启动时的所有值都将为空或null“我的ApplicationViewModel在其定义中已InotifyProperty更改,在我的setters中已RaiseProperty更改”——前者为真,后者为假。显示的所有属性都是隐式实现,例如,
public RelayCommand loadapplicationcommand{get;set;}
。“在我的XAML中,我将datacontext设置为我的申请者类”--不,您没有。您将
DataContext
属性设置为不存在的
applicator
属性值。同样,您是否查看了调试输出以查看存在哪些绑定错误?如果您需要帮助,您需要改进问题中的代码示例。此属性
applicator
在“DataContext=“{Binding applicator}”中使用的位置?它在哪里居住?