【问题标题】:WPF ViewModel Changes Not Updating ListviewWPF ViewModel 更改未更新 Listview
【发布时间】: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; &lt;Window.DataContext&gt;&lt;vw:UsersController/&gt;&lt;/Window.DataContext&gt; 为什么要设置两次数据源?有冲突吗?
  • 我尝试从 Mainwindow.cs 和 Mainwindow.xaml 绑定
  • 我创建了一个对象作为实体来进行 crud 操作

标签: c# wpf visual-studio mvvm data-binding


【解决方案1】:

看看我的最新回答:C# wpf listview binding problem with observable collection

这是一个基本示例,说明如何至少在开始时使用 listviews/treeviews/gridviews。如果您尝试它,那么您会将大部分代码移动到 ViewModel 部分,但随后您将搜索命令和它们之间的交互(棱镜可能没问题 - 我的意思是事件聚合,不要太快进行依赖注入)。

您的一个类名为 UsersController。当您切换到 WPF 时,请记住 MVC 模式不再存在,并且大部分都在 ViewModel 中的某个位置,因此它更像是 UserViewModel 或 UsersViewModel。如果我没看错,那么我是在告诉你,从经典的 MVC(各种实现)切换到某种正确、令人愉快的 MVVM 是很困难的。

我提供的 URL 少了一个主要的东西,就是标准的 ViewModelWrapper,通常称为 ViewModelBase 或 BaseViewModel,你自己调用吧。

它形成了(虽然它应该是我第一篇文章的一部分):

namespace MyNamespace.ViewModel
{
    public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName=null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = "")
        {
            if (EqualityComparer<T>.Default.Equals(storage, value))
                return false;
            storage = value;
            this.OnPropertyChanged(propertyName);
            return true;
        }

    }
}

【讨论】:

    【解决方案2】:

    首先,您应该删除以下 XAML,因为您正在以编程方式设置 DataContext

    <Window.DataContext>
        <vw:UsersController/>
    </Window.DataContext>
    

    其次,你应该调用UsersController的数据绑定实例的Increase()方法:

    private void Fetch_Data_Button_Click(object sender, RoutedEventArgs e)
    {
        controller1.Increase();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 2019-10-25
      • 2016-03-02
      相关资源
      最近更新 更多