【发布时间】:2023-04-03 12:52:01
【问题描述】:
当按下获取数据按钮时,UserControllers 中的 User_List 已更改,但主窗口中的列表视图未更新。我使用数据库作为 EntityFramework6.0 我分享给你 UserController.cs(Viewmodel),Users.cs,MainWindow.cs,MainWindow.xaml 我该如何解决这个问题?
MainWindows.cs
public partial class MainWindow : Window ,INotifyPropertyChanged
{
ViewModels.UsersController controller1;
public ObservableCollection<Users> user_List;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public ObservableCollection<Users> User_List
{
get { return user_List; }
set {lst1.ItemsSource=user_List = value;OnPropertyChanged("User_List"); }
}
public MainWindow()
{
InitializeComponent();
controller1 = new ViewModels.UsersController();
this.DataContext = controller1;
}
private void Fetch_Data_Button_Click(object sender, RoutedEventArgs e)
{
ViewModels.UsersController controller = new ViewModels.UsersController();
controller.Increase();
lst1.UpdateLayout();
}
}
MainWindow.xaml
Window x:Class="MSSQLServer.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:MSSQLServer"
xmlns:vw="clr-namespace:MSSQLServer.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" >
<Window.DataContext>
<vw:UsersController/>
</Window.DataContext>
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="Fetch_Data_Button" Click="Fetch_Data_Button_Click" Content="Fetch Data" Width="150" Height="40" FontSize="18" Margin="10,10"></Button>
</StackPanel>
<ListView x:Name="lst1" HorizontalAlignment="Center" VerticalAlignment="Center" Height="300" Width="600" ItemsSource="{Binding User_List,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding id,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="50"></GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="120"></GridViewColumn>
<GridViewColumn Header="Surname" DisplayMemberBinding="{Binding surname,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="120"></GridViewColumn>
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding age,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="120"></GridViewColumn>
<GridViewColumn Header="E-Mail" DisplayMemberBinding="{Binding email,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="120"></GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Grid>
</Window>
用户控制器.cs
public class UsersController :INotifyPropertyChanged
{
public ObservableCollection<Users> user_List;
public ObservableCollection<Users> User_List
{
get { return user_List; }
set { user_List = value;OnPropertyChanged("User_List"); }
}
public UsersController()
{
Increase();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public void Increase()
{
Database1Entities entities = new Database1Entities();
User_List = new ObservableCollection<Users>(entities.Users.ToList());
}
}
和Users.cs
public class Userss : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private int idd;
public int id
{
get { return idd; }
set { idd = value; OnPropertyChanged("id"); }
}
private string namee;
public string name
{
get { return namee; }
set { namee = value; OnPropertyChanged("name"); }
}
private string surnamee;
public string surname
{
get { return surnamee; }
set { surnamee = value; OnPropertyChanged("surname"); }
}
private int agee;
public int age
{
get { return agee; }
set { agee = value; OnPropertyChanged("age"); }
}
private string emaill;
public string email
{
get { return emaill; }
set { emaill = value; OnPropertyChanged("email"); }
}
}
【问题讨论】:
-
可以展示一下你的实体操作吗?以及数据库表设计?
-
Database1Entities entities = new Database1Entities();你能解释一下这段代码吗? -
controller1 = new ViewModels.UsersController();this.DataContext = controller1;<Window.DataContext><vw:UsersController/></Window.DataContext>为什么要设置两次数据源?有冲突吗? -
我尝试从 Mainwindow.cs 和 Mainwindow.xaml 绑定
-
我创建了一个对象作为实体来进行 crud 操作
标签: c# wpf visual-studio mvvm data-binding