WPF MVVM属性绑定无法正常工作

时间:2016-11-25 04:47:40

标签: c# wpf xaml mvvm

我创建了一个视图模型,它具有学生模型的单个属性,然后我将绑定到我的XAML中的控件。但是当我执行应用程序时,没有任何东西出现。

我在app.xaml.cs中设置数据上下文,如下所示:

 protected override void OnStartup(StartupEventArgs e)   
 {         
   base.OnStartup(e);

   Registrationformusinemvvm.MainWindow window = new MainWindow();

   VMUser VM = new VMUser();

   window.DataContext = VM;

   window.Show();

 }

为什么绑定不起作用?

这是我的观点模型:

   public class VMUser:BaseClass
   {    
     private student _currentStudent;

     public student CurrentStudent
     {
         get { return _currentStudent; }
         set { 
               _currentStudent = value; 
               OnPropertyChanged("CurrentStudent");    
         }
     }
   }

我的学生模特班:

 public class student:BaseClass
 {      
   private string name="sumit";

     public string Name
     {
         get { return name; }
         set { name = value; OnPropertyChanged("Name"); }
     }

     private int rollNum;

     public int RollNum
     {
         get { return rollNum; }
         set { rollNum = value;OnPropertyChanged("RollNum"); }
     }

     private int phNum;

     public int PhNum
     {
         get { return phNum; }
         set { phNum = value;OnPropertyChanged("PhNum"); }
     }
     private string sub;

     public string Sub
     {
         get { return sub; }
         set { sub = value;OnPropertyChanged("Sub"); }
     }

 }

我的XAML:

<Window x:Class="Registrationformusinemvvm.MainWindow"
    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:Registrationformusinemvvm"
    xmlns:vm="clr-namespace:Registrationformusinemvvm.ViewModel"
    mc:Ignorable="d"

    Title="MainWindow" Height="350" Width="525">
  <!--<Window.DataContext>
    <vm:VMUser/>
  </Window.DataContext>-->
  <Window.Resources>

 </Window.Resources>
 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Name"  Grid.Column="0" Grid.Row="0" FontSize="14"       

    FontWeight="Bold" VerticalAlignment="Center"   
    HorizontalAlignment="Center"/>
    <TextBlock Text="Roll Number" Grid.Column="0" Grid.Row="1" FontSize="14" 

   FontWeight="Bold" VerticalAlignment="Center"   

   HorizontalAlignment="Center"/>
    <TextBlock Text="Subject" Grid.Column="0" Grid.Row="2" FontSize="14" 

  FontWeight="Bold" VerticalAlignment="Center" 
  HorizontalAlignment="Center"/>
    <TextBlock Text="Phone Number" Grid.Column="0" Grid.Row="3"  
  FontSize="14" FontWeight="Bold" VerticalAlignment="Center" 
 HorizontalAlignment="Center"/>
    <TextBox Name="tbName" Text="{Binding CurrentStudent.Name,Mode=TwoWay,   
 UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="0" 
Width="120" Height="30" HorizontalAlignment="Center" 
VerticalAlignment="Center"/>
    <TextBox Name="tbRollnum" Text="{Binding CurrentStudent.RollNum}" 
Grid.Column="1" Grid.Row="1" Width="120" Height="30"   
HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <TextBox Name="tbSub" Text="{Binding CurrentStudent.Sub}" 
Grid.Column="1" Grid.Row="2" Width="120" Height="30" 
HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <TextBox Name="tbPh" Text="{Binding CurrentStudent.PhNum}" 
Grid.Column="1" Grid.Row="3" Width="120" Height="30" 
HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <Button Name="tbSubmit" Content="Submit" Grid.ColumnSpan="3" 
Grid.Row="4" Height="30" Width="100" HorizontalAlignment="Center"/>
</Grid>

</Window>

4 个答案:

答案 0 :(得分:1)

我的猜测是你的绑定不起作用,因为你的_currentStudent默认为null。如果为null,则初始化_currentStudent。

    public student CurrentStudent
    {
        get { return _currentStudent = (_currentStudent ?? new student()); }
        set
        {
            _currentStudent = value; OnPropertyChanged("CurrentStudent");
        }
    }

答案 1 :(得分:0)

您需要在模型类中添加OnPropertyChanged。

void OnPropertyChanged(string prop)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }

        public event PropertyChangedEventHandler PropertyChanged;

答案 2 :(得分:0)

根据您的上述代码,您无法将值分配给CurrentStudent Property 你能检查一下你对CurrentStudent财产的价值吗?

感谢您的提问

答案 3 :(得分:-1)

删除StartupUri =&#34; YourXamlFile.xaml&#34;来自 App.Xaml