DependencyProperty上的PropertyChangedCallback仅触发一次

编程入门 行业动态 更新时间:2024-10-20 05:35:40
本文介绍了DependencyProperty上的PropertyChangedCallback仅触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有确切的问题,因为这个在Silverlight论坛中的人和可接受的答案是:

I have the exact problem as this guy in the Silverlight Forum and the accepted answer is :

在这种情况下,您的财产实际上并未改变价值。您已将内容添加到列表中,但是该列表是相同的列表,因此当 DependencyProperty机制发现实际值(对列表中的引用)没有变化时,它没有t提高您的OnChanged处理程序

In this case, your property didn't actually change value. You added something to your List, but the list is the same List so when the DependencyProperty mechanism sees that the actual value (reference to your List) didn't change, it didn't raise your OnChanged handler

这是一个很好的说明,但不是解决此问题的答案。我可以在Google上找到许多有关WPF的建议,而不是有关Silverlight的建议。

This is a great explication but not an answer to fix this problem. I can find on Google many suggestion for WPF but not for Silverlight.

问题描述如下:您有一个DependencyProperty,在初始化变量时会调用它,但是之后便没有任何更新。

The problem is describe as this one : You have a DependencyProperty that is called when the variable is initialized but after then nothing is updated.

public partial class MyGrid : UserControl { public MyGrid() { InitializeComponent(); } public static readonly DependencyProperty ShapesProperty = DependencyProperty.Register( "Shapes", typeof(ObservableCollection<ModelItem>), typeof(MyGrid), new PropertyMetadata(OnShapesPropertyChanged)); public ObservableCollection<ModelItem> Shapes { private get { return (ObservableCollection<ModelItem>)GetValue(ShapesProperty); } set { SetValue(ShapesProperty, value); } } private static void OnShapesPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { ((MyGrid)o).OnShapesPropertyChanged(e); //Fire Only Once } private void OnShapesPropertyChanged(DependencyPropertyChangedEventArgs e) { dg.ItemsSource = e.NewValue as ObservableCollection<ModelItem>; } } //-------- public class ViewModel : INotifyPropertyChanged { public Model Model { get; set; } public RelayCommand cmd; public ObservableCollection<ModelItem> ModelItemCollection { get { return Model.ModelItem; } } public ViewModel() { Model = new Model(); Model.PropertyChanged += Model_PropertyChanged; } void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { System.Diagnostics.Debug.WriteLine(e.PropertyName); if (PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("ModelItemCollection")); } } public ICommand AddCmd { get { return cmd ?? (cmd = new RelayCommand(a => Model.ModelItem.Add(new ModelItem {Name = "asd"}))); } } public event PropertyChangedEventHandler PropertyChanged; } ///---------------------- public class Model: INotifyPropertyChanged { public ObservableCollection<ModelItem> ModelItem { get; set; } public Model() { ModelItem = new ObservableCollection<ModelItem>(); ModelItem.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ModelItem_CollectionChanged); } void ModelItem_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("ModelItem")); } } public event PropertyChangedEventHandler PropertyChanged; } public class ModelItem { public String Name { get; set; } }

即使显式调用PropertyChanged()也不会更新。

Even with explicit call of PropertyChanged() nothing is updated.

有什么变通办法来让ObservableCollection的元素已更改的DependencyProperty知道?

What is the workaround to let know the DependencyProperty that the ObservableCollection has elements that have changed?

推荐答案

伪代码:

BindingOperations.GetBindingExpressionBase(dependencyObject, dependencyProperty).UpdateTarget();

查看此处:强制将WPF绑定刷新 ...

尝试一下,通常可以:)

Try this, usually works :)

更多推荐

DependencyProperty上的PropertyChangedCallback仅触发一次

本文发布于:2023-11-27 12:23:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638057.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DependencyProperty   PropertyChangedCallback

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!