我的 UI 上显示了两个数据网格。当我在数据网格 1 上选择特定行时,我想在数据网格 2 上显示数据网格 1 的详细信息。我正在从数据库中填充数据网格数据。这里是两个数据库表结构。
注意:这两个表都是由数据库中的personid映射的
这是到目前为止我尝试过的代码
基类.cs
public class Baseclass
{
public event PropertyChangedEventHandler PropertyChanged;
protected void SetProperty<T>(ref T member, T value, [CallerMemberName] string propertyName = null)
{
member = value;
this.RaiseNotification(propertyName);
}
protected void RaiseNotification(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
个人.cs
public class person : Baseclass
{
private int personID;
public int PersonID
{
get { return personID; }
set { this.SetProperty<int>(ref this.personID, value); }
}
private string firstName;
public string FirstName
{
get { return firstName; }
set { this.SetProperty<string>(ref this.firstName, value); }
}
private string lastName;
public string LastName
{
get { return lastName; }
set { this.SetProperty<string>(ref this.lastName, value); }
}
Model _personModel = new Model();
private ObservableCollection<person> _person = new ObservableCollection<person>();
public ObservableCollection<person> Getpersons
{
get { return _person; }
set { _person = value; OnPropertyChanged("GetPersons"); }
}
public person()
{
initializeload();
}
private void initializeload()
{
try
{
DataTable table = _personModel.getData();
for (int i = 0; i < table.Rows.Count; ++i)
Getpersons.Add(new person
{
PersonID = Convert.ToInt32(table.Rows[i][0]),
FirstName = table.Rows[i][1].ToString(),
LastName = table.Rows[i][2].ToString(),
});
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
public class Model
{
public DataTable getData()
{
DataTable ndt = new DataTable();
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Person].[dbo].[persons]", sqlcon);
da.Fill(ndt);
da.Dispose();
sqlcon.Close();
return ndt;
}
}
}
PersonDetail 类
public class PersonDetails : Baseclass
{
private int personID;
public int PersonID
{
get { return personID; }
set { this.SetProperty<int>(ref this.personID, value); }
}
private string address;
public string Address
{
get { return address; }
set { this.SetProperty<string>(ref this.address, value); }
}
private string pos;
public string Position
{
get { return pos; }
set { this.SetProperty<string>(ref this.pos, value); }
}
DetailsModel _detailModel = new DetailsModel();
private ObservableCollection<PersonDetails> _details = new ObservableCollection<PersonDetails>();
public ObservableCollection<PersonDetails> GetDetails
{
get { return _details; }
set { _details = value; OnPropertyChanged("GetDetails"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
public PersonDetails()
{
initializeload();
}
private void initializeload()
{
try
{
DataTable table = _detailModel.getData();
for (int i = 0; i < table.Rows.Count; ++i)
GetDetails.Add(new PersonDetails
{
PersonID = Convert.ToInt32(table.Rows[i][0]),
Address = table.Rows[i][1].ToString(),
Position = table.Rows[i][2].ToString(),
});
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public class DetailsModel
{
public DataTable getData()
{
DataTable ndt = new DataTable();
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Person].[dbo].[personDetails]", sqlcon);
da.Fill(ndt);
da.Dispose();
sqlcon.Close();
return ndt;
}
}
}
主视图模型.cs
public class MainViewModel : Base, INotifyPropertyChanged
{
public MainViewModel()
{
}
private ObservableCollection<person> personValues;
public ObservableCollection<person> Persons
{
get { return personValues; }
set
{
this.SetProperty<ObservableCollection<person>>(ref this.personValues, value);
}
}
private ObservableCollection<PersonDetails> detailsValues;
public ObservableCollection<PersonDetails> Details
{
/* This is correct below ?? I have an error as
'PersonDemo.MainViewModel' does not contain a definition for 'GetDetails' and no extension method 'GetDetails' accepting a first argument of type 'PersonDemo.MainViewModel' could be found (are you missing a using directive or an assembly reference?)*/
get { return this.GetDetails(this.Selectedperson.PersonID); }
}
private person selectedValue;
public person Selectedperson
{
get { return selectedValue; }
set
{
this.SetProperty<person>(ref this.selectedValue, value);
this.RaiseNotification("Details");
}
}
}
XAML
<Grid>
<DataGrid Margin="100,20,116,211" ItemsSource="{Binding Persons}" SelectedItem="{Binding Selectedperson}" />
<DataGrid Margin="100,130,116,101" ItemsSource="{Binding Details}" />
</Grid>
任何人都可以帮助继续编写 MainViewModel 吗?几个星期以来我都被困在这里。