WPF绑定:如何在属性的属性发生更改时更改属性

时间:2010-07-02 07:14:22

标签: binding

我创建了课程:

class StorageBase
{
    public Queue<Slices> Slices {get;set;}
}

和wpf自定义控件,它具有StorageBase类型的依赖属性存储:

public StorageBase Storage
        {
            get { return (StorageBase)GetValue(StorageProperty); }
            set { SetValue(StorageProperty, value); }
        }
        public static readonly DependencyProperty StorageProperty =
            DependencyProperty.Register("Storage", typeof(StorageBase), typeof(MaterialStreamControl), new UIPropertyMetadata(null, new PropertyChangedCallback(OnStoragePropertyChanged)));
        static void OnStoragePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as MaterialStreamControl).Render();
        }

如果存储中的切片发生了变化,我怎么能重新渲染组件?

1 个答案:

答案 0 :(得分:1)

通常,StorageBase会实现INotifyPropertyChanged。然后,Slices上的setter将引发INotifyPropertyChanged.PropertyChanged事件。

示例:http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx

编辑:你可能还想让Slices成为ObservableCollection而不是Queue。 http://msdn.microsoft.com/en-us/library/ms668604.aspx