C# 绑定到ObservableCollection的文本框未更新

C# 绑定到ObservableCollection的文本框未更新,c#,wpf,xaml,binding,observablecollection,C#,Wpf,Xaml,Binding,Observablecollection,我有一个绑定到observablecollection的文本框,当我更新元素时(通过拖放触发视图文件中处理的事件),文本框不会更新其值。但是,数据被添加到drop上的observable集合中,如果我刷新数据(通过在列表框中实际选择不同的项并切换回当前记录),数据就会出现 我已经读过了:不,我不相信我在重写这个收藏 <StackPanel> <TextBox Text="{Binding Path=ImageGalleryFilenames, Converter={St

我有一个绑定到observablecollection的文本框,当我更新元素时(通过拖放触发视图文件中处理的事件),文本框不会更新其值。但是,数据被添加到drop上的observable集合中,如果我刷新数据(通过在列表框中实际选择不同的项并切换回当前记录),数据就会出现

我已经读过了:不,我不相信我在重写这个收藏

<StackPanel>
    <TextBox Text="{Binding Path=ImageGalleryFilenames, Converter={StaticResource ListToStringWithPipeConverter}}" Height="41" TextWrapping="Wrap" VerticalAlignment="Top"/>
    <Button Height="25" Margin="0 2" AllowDrop="True" Drop="HandleGalleryImagesDrop">
        <TextBlock Text="Drop Image Files Here"></TextBlock>
    </Button>
</StackPanel>

我添加到集合中的事实不足以更新绑定到observablecollection的文本框吗?或者我是否遗漏了一些非常明显的东西?

本质上,
textbox
无法知道绑定到
文本的集合已被更新。由于
Text
属性不侦听
CollectionChanged
事件,因此@Clemens指出,更新
observedcollection
也将被忽略

在ViewModel中,这是一种方法

    private ObservableCollection<ImageGalleryFilename> _imageGalleryFilenames;
    public ObservableCollection<ImageGalleryFilename> ImageGalleryFilenames
    {
        get
        {
            return _imageGalleryFilenames;
        }
        set
        {
            _imageGalleryFilenames= value;
            if (_imageGalleryFilenames!= null)
            {
                _imageGalleryFilenames.CollectionChanged += _imageGalleryFilenames_CollectionChanged;
            }
            NotifyPropertyChanged("ImageGalleryFilenames");
        }
    }

    private void _imageGalleryFilenames_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        NotifyPropertyChanged("ImageGalleryFilenames");
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    } 
私有ObservableCollection\u ImageGalleryFileName;
公共ObservableCollection ImageGalleryFileName
{
得到
{
返回_imagegalleryfilename;
}
设置
{
_imageGalleryFilenames=值;
如果(_imageGalleryFilenames!=null)
{
_imageGalleryFilenames.CollectionChanged+=\u imageGalleryFilenames\u CollectionChanged;
}
NotifyPropertyChanged(“ImageGalleryFileName”);
}
}
private void\u imageGalleryFilenames\u CollectionChanged(对象发送方,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
NotifyPropertyChanged(“ImageGalleryFileName”);
}
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
} 

是否为ImageGalleryFileName触发了属性更改事件?在处理拖放操作后..我相信您缺少RaisePropertyChanged(“ImageGalleryFilenames”);这是因为添加到集合不会触发
PropertyChanged
事件,而是触发
CollectionChanged
事件。只需为
ImageGalleryFilenames
调用已更改的属性就足够了。它甚至不能引发PropertyChanged事件,因为列表实例不会更改,因此绑定目标将自动忽略更改通知。另一方面,Text属性不侦听CollectionChanged事件,因此更新ObservableCollection也将被忽略。这是错误的方法。替换整个集合,或者向视图模型添加一个字符串属性(带有更改通知)并绑定到该属性。我想临时解决方案只是将一个新的ObservableCollection分配给变量,以触发setter中的notify属性。但是,根据我问题中的链接,我实际上并没有覆盖可观察集合的setter,而是清除它并将值中的项添加到当前集合中。
    private ObservableCollection<ImageGalleryFilename> _imageGalleryFilenames;
    public ObservableCollection<ImageGalleryFilename> ImageGalleryFilenames
    {
        get
        {
            return _imageGalleryFilenames;
        }
        set
        {
            _imageGalleryFilenames= value;
            if (_imageGalleryFilenames!= null)
            {
                _imageGalleryFilenames.CollectionChanged += _imageGalleryFilenames_CollectionChanged;
            }
            NotifyPropertyChanged("ImageGalleryFilenames");
        }
    }

    private void _imageGalleryFilenames_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        NotifyPropertyChanged("ImageGalleryFilenames");
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }