Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net INotifyPropertyChanged PropertyChanged事件保持为空_.net_Wpf_Mvvm_Data Binding_Inotifypropertychanged - Fatal编程技术网

.net INotifyPropertyChanged PropertyChanged事件保持为空

.net INotifyPropertyChanged PropertyChanged事件保持为空,.net,wpf,mvvm,data-binding,inotifypropertychanged,.net,Wpf,Mvvm,Data Binding,Inotifypropertychanged,我在论坛上搜索过,其他一些关于PropertyChanged事件的来源仍然为空,但没有找到任何解决方案。我用AModel、AViewModel和MainWindow创建了一个示例WPF应用程序。Model和ViewModel已实现INotifyPropertyChanged接口,但在运行时选中时,这两个PropertyChanged事件仍为null。申请代码如下: public class AModel : INotifyPropertyChanged { public AModel()

我在论坛上搜索过,其他一些关于PropertyChanged事件的来源仍然为空,但没有找到任何解决方案。我用AModel、AViewModel和MainWindow创建了一个示例WPF应用程序。Model和ViewModel已实现INotifyPropertyChanged接口,但在运行时选中时,这两个PropertyChanged事件仍为null。申请代码如下:

public class AModel : INotifyPropertyChanged
{
    public AModel()
    {

    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyname = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
        }
    }
    private string _A;
    public string A
    {
        get
        {
            return _A;
        }
        set
        {
            _A = value;
            NotifyPropertyChanged("A");
        }
    }
}

public class AViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyname = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
        }
    }
    public AViewModel()
    {
        MyList = new List<AModel>();

    }

    private List<AModel> _MyList;
    public List<AModel> MyList
    {
        get
        {
            return _MyList;
        }
        set
        {
            _MyList = value;
        }
    }


}

public partial class MainWindow : Window
{
    private AViewModel _objViewModel=new AViewModel();
    public AViewModel ObjViewModel
    {
        get
        {
            return _objViewModel;
        }
        set
        {
            _objViewModel = value;
        }
    }
    public MainWindow()
    {
        InitializeComponent();
        ObjViewModel = new AViewModel();
        DataContext = ObjViewModel;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ObjViewModel.MyList.Add(new AModel() { A = "Sample Text" });
    }
}
公共类AModel:INotifyPropertyChanged
{
公共模型()
{
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyname=“”)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyname));
}
}
私有字符串_A;
公共字符串A
{
收到
{
返回A;
}
设置
{
_A=数值;
通知财产变更(“A”);
}
}
}
公共类AViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyname=“”)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyname));
}
}
公共航空模型()
{
MyList=新列表();
}
私人名单;
公共列表列表
{
收到
{
返回我的列表;
}
设置
{
_MyList=值;
}
}
}
公共部分类主窗口:窗口
{
私有AViewModel_objViewModel=新的AViewModel();
公共AViewModel ObjViewModel
{
收到
{
返回_objViewModel;
}
设置
{
_objViewModel=值;
}
}
公共主窗口()
{
初始化组件();
ObjViewModel=新的AViewModel();
DataContext=ObjViewModel;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
添加(new AModel(){A=“Sample Text”});
}
}
现在,当我向模型类PropertyChanged事件添加断点时,它始终为null。我已经创建了视图模型的对象,并将其分配给MainWindow的DataContext,但仍然为空。我无法理解在这个简单的应用程序中我哪里出错了。我检查了装订是否正常


提前感谢

PropertyChanged事件在此处为空,因为该属性实际上没有更改。您正在创建集合,然后将其绑定到UI控件。如果希望看到已更改的属性被触发,则可以尝试以下操作:-

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <DataGrid Margin="20" AutoGenerateColumns="True" ItemsSource="{Binding MyList}"/>
        <ListBox Grid.Row="1" ItemsSource="{Binding MyList}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding A,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button  Grid.Row="2" HorizontalAlignment="Center" Margin="20" Content="Click Me" Click="Button_Click"/>
    </Grid>

如果您需要更多说明,请发表评论。

您在视图中使用
MyList
做什么?只要视图没有绑定到该集合的AModel元素的属性,其PropertyChanged事件当然将为null。为了使
ObjViewModel.MyList.Add
更新视图,请使用ObservableCollection,即
public ObservableCollection MyList
@Clemens MyList用于绑定主窗口中的数据网格,AModel属性a与该数据网格列绑定。我知道可观察集合,但我想找出如果DataContext被分配给MainWindow,属性更改事件为何保持为空的问题。解释完全错误。PropertyChanged事件不是空的,因为该属性在UI中没有更改。它肯定是空的,因为没有绑定。我之所以这么说是因为我们没有看到XAML,也不能确定。无论如何,请注意,
UpdateSourceTrigger=PropertyChanged
与INotifyPropertyChanged接口的
PropertyChanged
事件无关。这是两个不同的东西,碰巧有相同的名字。是的。UpdateSourceTrigger与已更改的属性无关。在本例中,调用ObjViewModel.MyList.Add(new AModel(){A=“Sample Text”});PropertyChanged为null,因为绑定仍然不存在。它只是一个创建的普通POCO,因此PropertyChanged为null。但是当绑定已经存在时,就会附加一个PropertyChanged,这就是它工作的原因。我提供的示例说明了这一点。糟糕的是@Clemens只注意到了PropertyChanged而没有注意到解决方案。因此,Pankaj,当您没有可用的绑定时,将不会触发更改的属性。在您的例子中,您提到您正在将它绑定到DataGrid,并且它显示良好,但是,它是在将该项添加到列表之后。PropertyChanged事件在UI中显示后将被附加。在我给出的示例中,我在呈现UI后在模板中更新它,因此PropertyChanged不为null。update source触发器只提到何时立即更新属性,或在失去焦点或显式时更新属性,每个控件都有默认方式。@Clemens,关于您的评论“PropertyChanged事件不是空的,只是因为属性在UI中没有更改。它肯定是空的,因为没有绑定。”即使有绑定,更改的属性最初可以为null。呈现控件时,将附加事件PropertyChanged。呈现控件后,属性中的任何更改都将使PropertyChanged事件不为null。
<DataTemplate>
                    <TextBox Text="{Binding A,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
{Method = {Void OnPropertyChanged(System.Object, System.ComponentModel.PropertyChangedEventArgs)}}