C# 数据不显示在视图上

C# 数据不显示在视图上,c#,wpf,mvvm,C#,Wpf,Mvvm,我制作了一个wpf应用程序来实现MVVM体系结构。我可以看到表格,但表格中没有加载数据,需要帮助。我正在使用Visual studio 2012 代码如下: 型号:-User.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace TestApplication.Model {

我制作了一个wpf应用程序来实现MVVM体系结构。我可以看到表格,但表格中没有加载数据,需要帮助。我正在使用Visual studio 2012

代码如下:

型号:-User.cs

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace TestApplication.Model
{
        public class User : INotifyPropertyChanged
        {
            private int userId;
            private string firstName;
            private string lastName;
            private string city;
            private string state;
            private string country;

            public int UserId
            {
                get
                {
                    return userId;
                }
                set
                {
                    userId = value;
                    OnPropertyChanged("UserId");
                }
            }
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    firstName = value;
                    OnPropertyChanged("FirstName");
                }
            }
            public string LastName
            {
                get
                {
                    return lastName;
                }
                set
                {
                    lastName = value;
                    OnPropertyChanged("LastName");
                }
            }
            public string City
            {
                get
                {
                    return city;
                }
                set
                {
                    city = value;
                    OnPropertyChanged("City");
                }
            }
            public string State
            {
                get
                {
                    return state;
                }
                set
                {
                    state = value;
                    OnPropertyChanged("State");
                }
            }
            public string Country
            {
                get
                {
                    return country;
                }
                set
                {
                    country = value;
                    OnPropertyChanged("Country");
                }
            }

            #region INotifyPropertyChanged Members
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
        }  
}
视图:-MainPage.xml

<Window x:Class="TestApplication.View.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TestApplication" Height="485" Width="525">
    <Grid Margin="0,0,0,20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListView Name="UserGrid" Grid.Row="1" Margin="4,178,12,13"  ItemsSource="{Binding Users}"  >
            <ListView.View>
                <GridView x:Name="grdTest">
                    <GridViewColumn Header="UserId" DisplayMemberBinding="{Binding UserId}"  Width="50"/>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"  Width="80" />
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="100" />
                    <GridViewColumn Header="City" DisplayMemberBinding="{Binding City}" Width="80" />
                    <GridViewColumn Header="State" DisplayMemberBinding="{Binding State}" Width="80" />
                    <GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" Width="100" />
                </GridView>
            </ListView.View>
        </ListView>
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,7,0,0" Name="txtUserId" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid,Path=SelectedItem.UserId}" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,35,0,0" Name="txtFirstName" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid,Path=SelectedItem.FirstName}" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,62,0,0" Name="txtLastName" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid,Path=SelectedItem.LastName}" />
        <Label Content="UserId" Grid.Row="1" HorizontalAlignment="Left" Margin="12,12,0,274" Name="label1" />
        <Label Content="Last Name" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,60,0,0" Name="label2" VerticalAlignment="Top" />
        <Label Content="First Name" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,35,0,0" Name="label3" VerticalAlignment="Top" />
        <Button Content="Update" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="310,40,0,0" Name="btnUpdate" 
                VerticalAlignment="Top" Width="141"
                Command="{Binding Path=UpdateCommad}"  />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,143,0,0" x:Name="txtCity" VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.City, ElementName=UserGrid}" />
        <Label Content="Country" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,141,0,0" x:Name="label2_Copy" VerticalAlignment="Top" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,88,0,0" x:Name="txtCountry" VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.Country, ElementName=UserGrid}" />
        <Label Content="City" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,86,0,0" x:Name="label2_Copy1" VerticalAlignment="Top" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,115,0,0" x:Name="txtSTate" VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.State, ElementName=UserGrid}" />
        <Label Content="State" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,113,0,0" x:Name="label2_Copy2" VerticalAlignment="Top" />
    </Grid>
</Window>

视图模型:UserViewMOdel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using TestApplication.Model;
using System.Windows.Input;

namespace TestApplication.ViewModel
{
    class UserViewModel
    {
        private IList<User> _UsersList;
        public UserViewModel()
        {
            _UsersList = new List<User>
            {
                new User{UserId = 1,FirstName="Raj",LastName="Beniwal",City="Delhi",State="DEL",Country="INDIA"},
                new User{UserId=2,FirstName="Mark",LastName="henry",City="New York", State="NY", Country="USA"},
                new User{UserId=3,FirstName="Mahesh",LastName="Chand",City="Philadelphia", State="PHL", Country="USA"},
                new User{UserId=4,FirstName="Vikash",LastName="Nanda",City="Noida", State="UP", Country="INDIA"},
                new User{UserId=5,FirstName="Harsh",LastName="Kumar",City="Ghaziabad", State="UP", Country="INDIA"},
                new User{UserId=6,FirstName="Reetesh",LastName="Tomar",City="Mumbai", State="MP", Country="INDIA"},
                new User{UserId=7,FirstName="Deven",LastName="Verma",City="Palwal", State="HP", Country="INDIA"},
                new User{UserId=8,FirstName="Ravi",LastName="Taneja",City="Delhi", State="DEL", Country="INDIA"}            
            };
        }
        public IList<User> Users
        {
            get { return _UsersList; }
            set { _UsersList = value; }
        }
        private ICommand mUpdater;
        public ICommand UpdateCommand
        {
            get
            {
                if (mUpdater == null)
                    mUpdater = new Updater();
                return mUpdater;
            }
            set
            {
                mUpdater = value;
            }
        }
        private class Updater : ICommand
        {
            #region ICommand Members
            public bool CanExecute(object parameter)
            {
                return true;
            }
            public event EventHandler CanExecuteChanged;
            public void Execute(object parameter)
            {
            }
            #endregion
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统组件模型;
使用TestApplication.Model;
使用System.Windows.Input;
命名空间TestApplication.ViewModel
{
类UserViewModel
{
私有IList_用户列表;
公共用户视图模型()
{
_UsersList=新列表
{
新用户{UserId=1,FirstName=“Raj”,LastName=“Beniwal”,City=“德里”,State=“DEL”,Country=“印度”},
新用户{UserId=2,FirstName=“Mark”,LastName=“henry”,City=“new York”,State=“NY”,Country=“USA”},
新用户{UserId=3,FirstName=“Mahesh”,LastName=“Chand”,City=“Philadelphia”,State=“PHL”,Country=“USA”},
新用户{UserId=4,FirstName=“Vikash”,LastName=“Nanda”,City=“Noida”,State=“UP”,Country=“INDIA”},
新用户{UserId=5,FirstName=“Harsh”,LastName=“Kumar”,City=“Ghaziabad”,State=“UP”,Country=“INDIA”},
新用户{UserId=6,FirstName=“Reetesh”,LastName=“Tomar”,City=“孟买”,State=“MP”,Country=“印度”},
新用户{UserId=7,FirstName=“Deven”,LastName=“Verma”,City=“Palwal”,State=“HP”,Country=“INDIA”},
新用户{UserId=8,FirstName=“Ravi”,LastName=“Taneja”,City=“德里”,State=“DEL”,Country=“印度”}
};
}
公共IList用户
{
获取{return\u UsersList;}
设置{u UsersList=value;}
}
私人ICommand mUpdater;
公共ICommand更新命令
{
得到
{
if(mUpdater==null)
mUpdater=新的更新程序();
返回mUpdater;
}
设置
{
mUpdater=值;
}
}
私有类更新程序:ICommand
{
#区域ICommand成员
公共布尔CanExecute(对象参数)
{
返回true;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
}
#端区
}
}
}

您需要初始化视图的DataContext。您可以在主页的ctor中执行此操作

这里我假设
UserViewModel
MainPage
的视图模型

public MainPage() {
  InitializeComponents();

  this.DataContext = new UserViewModel();
}

您需要初始化视图的DataContext。您可以在主页的ctor中执行此操作

这里我假设
UserViewModel
MainPage
的视图模型

public MainPage() {
  InitializeComponents();

  this.DataContext = new UserViewModel();
}

给出正确的答案。在给定正确的应答器时,他们没有设置DataContext.com。他们没有设置数据上下文。