我知道很多次都会问过这类问题。但我试图在不使用InotifyProperty或其他任何东西的情况下实现这一目标。我只想要用于显示模型数据的简单代码。 为此,我尝试使用以下方法绑定Datagrid。 我有一个模型:
public class PrimaryModel
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
private string _userName;
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
private string _password;
public string Password
{
get { return _password; }
set { _password = value; }
}
private DateTime _createdDateTime;
public DateTime CreatedDateTime
{
get { return _createdDateTime; }
set { _createdDateTime = value; }
}
private DateTime _lastLoginDateTime;
public DateTime LastLoginDateTime
{
get { return _lastLoginDateTime; }
set { _lastLoginDateTime = value; }
}
private bool _isActive;
public bool IsActive
{
get { return _isActive; }
set { _isActive = value; }
}
}
ViewModel:
public class PrimaryViewModel
{
private ObservableCollection<PrimaryModel> _UsersList;
public PrimaryViewModel()
{
_UsersList = new ObservableCollection<PrimaryModel>
{
new PrimaryModel { ID=1,UserName="Raghava",Password="Something",CreatedDateTime=DateTime.Now,LastLoginDateTime=DateTime.Now,IsActive=true }
};
}
public ObservableCollection<PrimaryModel> Users
{
get { return _UsersList; }
set { _UsersList = value; }
}
}
和一个XAML文件:
<Window x:Class="Sample4.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:Sample4"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="usersData" ItemsSource="{Binding Source=_UsersList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ID}" />
<DataGridTextColumn Binding="{Binding Path=UserName}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
如何通过ViewModel绑定DataGrid以显示基本ID和用户名?
答案 0 :(得分:0)
更改XAMl代码中的绑定,如ItemsSource="{Binding Source=Users}"
。你不能绑定到私人领域;你必须使用公共财产。
您还需要设置视图的DataContext
。你做到了吗?
但没有INotifyProperyChanged
的MVVM和绑定是一个糟糕的选择。当ViewModel发生变化时,您的视图将不会更新。
答案 1 :(得分:0)
public class PrimaryViewModel
{
public ObservableCollection<PrimaryModel> Users //Allways private set - to not destroy the Binding! Use Clear instead of reintializing !!!
{
get;
private set;
}
public PrimaryViewModel()
{
Users = new ObservableCollection<PrimaryModel>
{
new PrimaryModel { ID=1,UserName="Raghava",Password="Something",CreatedDateTime=DateTime.Now,LastLoginDateTime=DateTime.Now,IsActive=true }
};
}
}
你主要设置ItemSource
错误...
<DataGrid Name="usersData" ItemsSource="{Binding Path=Users}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ID}"/>
<DataGridTextColumn Binding="{Binding Path=UserName}" />
</DataGrid.Columns>
</DataGrid>
因为您不想使用INotifiyPropertyChanged
更新您的内容 - 将所有Setter
设置为私有,因为您的DataGrid中的字段当前不会更新。
另外,为什么要在所有属性中包装所有字段而不用其他内容!?!
答案 2 :(得分:0)
您应该将视图的[uwsgi]
master = true
socket = /tmp/uwsgi.sock
chmod-socket = 666
chdir = /var/www/django/webserver_test
wsgi-file = /var/www/django/webserver_test/Django_app/wsgi.py
virtualenv = /var/www/django/virtual
pythonpath = /var/www/django/virtual/bin/python
vacuum = true
enable-threads = true
daemonize= /var/www/django/uwsgi.log
设置为视图模型的实例。
您可以在视图的代码隐藏中执行此操作:
DataContext
...或在XAML标记中:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new PrimaryViewModel();
}
}