DependencyProperty上的PropertyChangedCallback仅触发一次

时间:2011-09-07 19:35:49

标签: c# silverlight dependency-properties

我遇到了这个guy in the Silverlight Forum的确切问题,接受的答案是:

  

在这种情况下,您的财产实际上并没有改变价值。你补充说   列表中的内容,但列表是相同的列表所以当时   DependencyProperty机制看到实际值(引用   你的List没有改变,它没有提高你的OnChanged处理程序

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

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

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(),也不会更新任何内容。

告知DependencyProperty ObservableCollection具有已更改的元素的解决方法是什么?

1 个答案:

答案 0 :(得分:1)

伪代码:

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

请看这里:forcing a WPF binding to 'refresh' ...

试试这个,通常有效:)