【问题标题】:TextBox bound to ObservableCollection not updating绑定到 ObservableCollection 的文本框未更新
【发布时间】:2016-04-20 22:51:19
【问题描述】:

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

我已阅读:http://updatecontrols.net/doc/tips/common_mistakes_observablecollection,不,我不相信我正在覆盖集合!

<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>

这是我的事件代码,用于处理用户控件在视图文件中的放置。

    private void HandleGalleryImagesDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            var filenames = (string[])e.Data.GetData(DataFormats.FileDrop);

            foreach (var fn in filenames)
            {
                this.vm.CurrentSelectedProduct.ImageGalleryFilenames.Add(fn);
            }
        }
    }

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

【问题讨论】:

  • 是否为 ImageGalleryFilenames 触发了属性更改事件?处理拖放后..
  • 我相信您缺少 RaisePropertyChanged("ImageGalleryFilenames");
  • 这是因为添加到集合不会触发PropertyChanged 事件,它会触发CollectionChanged 事件。只需为ImageGalleryFilenames 调用属性更改就足够了。
  • 即使引发 PropertyChanged 事件也不起作用,因为列表实例不会更改,因此绑定目标将默默地忽略更改通知。另一方面,Text 属性不监听 Co​​llectionChanged 事件,因此更新 ObservableCollection 也将被忽略。这是错误的方法。要么替换整个集合,要么添加一个字符串属性(带有更改通知)到您的视图模型并绑定到它。
  • 我想一个临时的解决方案就是给变量分配一个新的 ObservableCollection 来触发 setter 中的 notify 属性。但是,根据我问题中的链接,我实际上并没有在 setter 中覆盖可观察集合,而只是清除它并将值中的项目添加到当前集合中。

标签: c# wpf xaml binding observablecollection


【解决方案1】:

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

在您的 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));
        }
    } 

【讨论】:

    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多