[英]How to display foreign key data value from database to TextBlock on while update form in WPF (MVVM / Entity Framework)?
[英]Hello, tell me how to implement data output from a database using Entity Framework in a Listbox WPF MVVM Pattern
能够通过 SelectItem 查看详细信息...我不明白如何正确绑定它们... 数据专用列表框 output... 而且我需要当您单击侧面的菜单项时,来自数据库的信息出现
列表框的 XAML
<ListBox Height="800" ItemsSource="{Binding ProfilesList}"></ListBox>
我的输出类
using ExperimentProjectt.DB_DIRECT;
using ExperimentProjectt.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExperimentProjectt.Services
{
public class ProfilesSystem
{
public DBContextClass _context = new();
public List<User> GetAllUsers ()
{
List<User> allusers = _context.Userss.ToList();
return allusers;
}
}
}
我的视图模型:
using ExperimentProjectt.DB_DIRECT;
using ExperimentProjectt.Entities;
using ExperimentProjectt.Services;
using ExperimentProjectt.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExperimentProjectt.ViewModels
{
public class ProfilesViewModel:ViewModelBase
{
private readonly ProfilesSystem _profilesSystem;
public ProfilesViewModel()
{
_profilesSystem = new ProfilesSystem();
}
private List<User> profileslist;
public List<User> ProfilesList
{
get
{
return profileslist = _profilesSystem.GetAllUsers();
}
set
{
profileslist = value;
OnPropertyChanged();
}
}
}
}
ObservableCollection 已经有接口:INotifyCollectionChanged、INotifyPropertyChanged。
public ObservableCollection<User> ProfilesList;
ProfilesList = new ObservableCollection<User>(_profilesSystem.GetAllUsers());
private User selectedItem;
public User SelectedItem
{
get { return selectedItem; }
set
{
selectedItem = value;
OnPropertyChanged();
}
}
然后:
<ListBox SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding ProfilesList}"></ListBox>
output 数据示例:
<StackPanel>
<TextBlock Text="{Binding SelectedItem.Name}"/>
<TextBlock Text="{Binding SelectedItem.Username}"/>
<TextBlock Text="{Binding SelectedItem.Age}"/>
</StackPanel>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.