我在後臺代碼中設置數據上下文,並在XAML中設置綁定。 調試顯示我的數據上下文正在從我的模型中填充,但這並未反映在我的視圖中。MVVM綁定未顯示在視圖中
大概是簡單的,但這一直困擾着我好幾個小時。
public partial class MainWindow : Window
{
public MainWindow(MainWindowVM MainVM)
{
this.DataContext = MainVM;
InitializeComponent();
}
}
public class MainWindowVM : INotifyPropertyChanged
{
private ICommand m_ButtonCommand;
public User UserModel = new User();
public DataAccess _DA = new DataAccess();
public MainWindowVM(string email)
{
UserModel = _DA.GetUser(UserModel, email);
//ButtonCommand = new RelayCommand(new Action<object>(ShowMessage));
}
}
public class User : INotifyPropertyChanged
{
private int _ID;
private string _FirstName;
private string _SurName;
private string _Email;
private string _ContactNo;
private List<int> _allocatedLines;
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
OnPropertyChanged("FirstName");
}
}
}
<Label Content="{Binding Path=FirstName}" HorizontalAlignment="Right" VerticalAlignment="Top" Padding="0,0,150,0"/>
您是否在任何地方綁定到'UserModel'? – Domysee
Content =「{Binding Path = UserModel.FirstName}」否? – tim