Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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
C# 可观察到的集合对它没有反应';s collecion_C#_Wpf - Fatal编程技术网

C# 可观察到的集合对它没有反应';s collecion

C# 可观察到的集合对它没有反应';s collecion,c#,wpf,C#,Wpf,我的列表框对我的可观察收集没有反应 这就是我说的列表框 <ListBox x:Name="CreateFieldsList" HorizontalAlignment="Left" Height="218" VerticalAlignment="Top" Width="244" Margin="0,86,0,0" BorderBrush="#FFB9B9B9"> <

我的列表框对我的可观察收集没有反应

这就是我说的列表框

<ListBox x:Name="CreateFieldsList"
         HorizontalAlignment="Left"
         Height="218"
         VerticalAlignment="Top"
         Width="244"
         Margin="0,86,0,0"
         BorderBrush="#FFB9B9B9">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Margin="4"
                  Width="215"
                  Height="32.96"
                  Background="White">
                <TextBlock Text="{Binding Name}"
                           FontWeight="Normal"
                           FontSize="18.667"
                           Padding="8,3,0,0" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
最后是视图项

public class ViewItem : INotifyPropertyChanged
{
    private event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged([CallerMemberName] string caller = "")
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(caller));
    }

    //The interface forces me to implement this. Why?
    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
    {
        add { }
        remove { }
    }
}

我不知道为什么这不起作用。您能提供帮助吗?

INotifyPropertyChanged接口需要您实现一个事件。事件实现无法工作,因为由于添加和删除块为空,注册和注销被忽略

在不添加和删除的情况下执行事件:

public class ViewItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged([CallerMemberName] string caller = "")
    {
        var copy = PropertyChanged;
        if (copy != null) 
            copy(this, new PropertyChangedEventArgs(caller));
    }
}

在将值分配给Itemsource之前,您尚未初始化
\u fieldItems
。抱歉,我意外删除了该行。我确实初始化了列表。仍然没有解决方案。但是列表是空的。并且还指定哪些不起作用?您预期的行为或结果是什么,以及您的程序当前的行为如何。当我这样做时,
RaisePropertyChanged
方法说
PropertyChanged只能出现在+=或-=
的左侧,并且
事件属性必须同时具有添加和删除访问器
public class ViewItem : INotifyPropertyChanged
{
    private event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged([CallerMemberName] string caller = "")
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(caller));
    }

    //The interface forces me to implement this. Why?
    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
    {
        add { }
        remove { }
    }
}
public class ViewItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged([CallerMemberName] string caller = "")
    {
        var copy = PropertyChanged;
        if (copy != null) 
            copy(this, new PropertyChangedEventArgs(caller));
    }
}