Wpf 已调用OnPropertyChange,但未对UI产生任何影响

Wpf 已调用OnPropertyChange,但未对UI产生任何影响,wpf,mvvm,Wpf,Mvvm,当我设置ViewModel的属性时(我指的是实现INotifyPropertyChanged并且是视图的DataContext的类),OnPropertyChange被调用,但它只是跳过“handler(this,newPropertyChangedEventArgs(name)”,因为PropertyChanged为null,因此UI没有更新。下面是我的代码: XAML: 视图模型: <!-- language: c# --> public class Vehic

当我设置ViewModel的属性时(我指的是实现INotifyPropertyChanged并且是视图的DataContext的类),OnPropertyChange被调用,但它只是跳过“handler(this,newPropertyChangedEventArgs(name)”,因为PropertyChanged为null,因此UI没有更新。下面是我的代码:

XAML:

视图模型:

    <!-- language: c# -->

    public class VehicleBase: INotifyPropertyChanged 
    {
        public VehicleBase()
        {
            //Product_Type = product_type;
            SetLists();
            Product_Type = "Structure";
        }


        private void SetLists()
        {
            SetList_Product_Type();
            SetList_Currency();
            SetList_PC_Class();
        }



        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }


        //ProductType
        private List<string> _product_Type_List = new List<string>();
        public List<string> Product_Type_List
        {
            get
            {
                return _product_Type_List;
            }
            set
            {
                _product_Type_List = value;
                OnPropertyChanged("Product_Type_List");
            }
        }
        private void SetList_Product_Type()
        {
            string query = "SELECT * FROM dbo.product_types WHERE is_enabled = 1";
            DataView dv = Utility.DAL.ExecuteQuery(query);
            List<string> l = new List<string>();
            for (int i = 0; i < dv.Count; i++)
            {
                l.Add(dv[i]["product_type_name"].ToString());
            }
            Product_Type_List = l;

        }

        private string _product_type;
        public string Product_Type
        {
            get
            {
                return _product_type;
            }
            set
            {
                _product_type = value;
                OnPropertyChanged("Product_Type");
            }
        }

        // Vehicle Name
        private string _vehicle_Name;
        public string Vehicle_Name
        {
            get
            {
                return _vehicle_Name;
            }
            set
            {
                _vehicle_Name = value;
                OnPropertyChanged("Vehicle_Name");

            }
        }
     }
公共类车辆库:InotifyProperty已更改
{
公共车辆基地()
{
//产品类型=产品类型;
集合列表();
产品类型=“结构”;
}
私有void集合列表()
{
SetList_Product_Type();
SetList_Currency();
SetList_PC_Class();
}
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
//产品类型
私有列表_产品_类型_列表=新列表();
公共列表产品类型列表
{
得到
{
返回产品类型列表;
}
设置
{
_产品类型列表=值;
OnPropertyChanged(“产品类型列表”);
}
}
私有无效集合列表\产品\类型()
{
string query=“从dbo.product\u类型中选择*,其中已启用\u=1”;
DataView dv=Utility.DAL.ExecuteQuery(查询);
列表l=新列表();
对于(int i=0;i
视图模型:

    <!-- language: c# -->

    public class VehicleBase: INotifyPropertyChanged 
    {
        public VehicleBase()
        {
            //Product_Type = product_type;
            SetLists();
            Product_Type = "Structure";
        }


        private void SetLists()
        {
            SetList_Product_Type();
            SetList_Currency();
            SetList_PC_Class();
        }



        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }


        //ProductType
        private List<string> _product_Type_List = new List<string>();
        public List<string> Product_Type_List
        {
            get
            {
                return _product_Type_List;
            }
            set
            {
                _product_Type_List = value;
                OnPropertyChanged("Product_Type_List");
            }
        }
        private void SetList_Product_Type()
        {
            string query = "SELECT * FROM dbo.product_types WHERE is_enabled = 1";
            DataView dv = Utility.DAL.ExecuteQuery(query);
            List<string> l = new List<string>();
            for (int i = 0; i < dv.Count; i++)
            {
                l.Add(dv[i]["product_type_name"].ToString());
            }
            Product_Type_List = l;

        }

        private string _product_type;
        public string Product_Type
        {
            get
            {
                return _product_type;
            }
            set
            {
                _product_type = value;
                OnPropertyChanged("Product_Type");
            }
        }

        // Vehicle Name
        private string _vehicle_Name;
        public string Vehicle_Name
        {
            get
            {
                return _vehicle_Name;
            }
            set
            {
                _vehicle_Name = value;
                OnPropertyChanged("Vehicle_Name");

            }
        }
     }

公共类车辆库:InotifyProperty已更改
{
公共车辆基地()
{
//产品类型=产品类型;
集合列表();
产品类型=“结构”;
}
私有void集合列表()
{
SetList_Product_Type();
SetList_Currency();
SetList_PC_Class();
}
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
//产品类型
私有列表_产品_类型_列表=新列表();
公共列表产品类型列表
{
得到
{
返回产品类型列表;
}
设置
{
_产品类型列表=值;
OnPropertyChanged(“产品类型列表”);
}
}
私有无效集合列表\产品\类型()
{
string query=“从dbo.product\u类型中选择*,其中已启用\u=1”;
DataView dv=Utility.DAL.ExecuteQuery(查询);
列表l=新列表();
对于(int i=0;i
DataContext实例和MainWindow中使用的实例。Xaml不同,PropertyChanged将仅连接到绑定到UI的实例。我认为您这样做是因为您面临按钮单击的问题,以及在ViewModel中处理按钮时面临的问题,如果是这种情况,请在ViewModel中实现ICommand以处理按钮单击事件,在这种情况下,您的所有代码都将在一个位置,您可以很容易地操作数据。另外,您必须创建ViewModel SingleTon类,并且必须在每个位置使用相同的实例

还有一个观察。请使用ObservaleCollection而不是List,因为List未实现INotifyPropertyChanged,因此当您添加新项时,它不会反映在as ObservaleCollection实现INotifyPropertyChanged的UI中

请参阅以下链接


您可以在MainWindow.Xaml.cs中更改如下内容

private VehicleBase _selected_vehicle = new VehicleBase();

public MainWindow()
{
    InitializeComponent();
    DataContext=_selected_vehicle;
}
保留剩下的代码,不需要更改。从Xmal文件中重新移动DataContext。这将解决您的问题,无需将Viewmodel设置为SingleTon

或者,如果您只想将dataContext设置为网格,那么您可以为网格指定一个名称,比如说grid1,并将dataContext设置为如下,而不是设置
public MainWindow()
{
    InitializeComponent();
    grid1.DataContext=_selected_vehicle;
}