wpf:依赖项属性如何相互通信

时间:2019-02-01 05:29:56

标签: c# wpf dependency-properties

依赖项属性如何相互通信。

我在一个类中有两个依赖项属性

public bool SaveToStream
        {
            get { return (bool)GetValue(SaveToStreamProperty); }
            set { SetValue(SaveToStreamProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SaveToStream.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SaveToStreamProperty =
            DependencyProperty.Register("SaveToStream", typeof(bool), typeof(PdfViewerControlHelper), new PropertyMetadata(OnSaveToStreamChanged));

        private static void OnSaveToStreamChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

            //how to i populate the contents of the ExtractedData here.
        }


        public ObservableCollection<DataItem> ExtractedData
        {
            get { return (ObservableCollection<DataItem>)GetValue(ExtractedDataProperty); }
            set { SetValue(ExtractedDataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ExtractedData.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ExtractedDataProperty =
            DependencyProperty.Register("ExtractedData", typeof(ObservableCollection<DataItem>), typeof(PdfViewerControlHelper),
                new FrameworkPropertyMetadata(new ObservableCollection<DataItem>(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null));

我有两个依赖项属性。 SaveToStream是一个布尔值,而ExtractedData是一个可观察的集合。

我要的是,是否在SaveToStream中进行了更改。我想在ExtractedData集合中进行填充。但是在我的OnSaveToStreamChanged方法中。我无法访问ExtractedData集合。我该如何解决?谢谢。

1 个答案:

答案 0 :(得分:2)

传递给PropertyChangedCallback的第一个参数是控件实例:

private static void OnSaveToStreamChanged(
    DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var control = (PdfViewerControlHelper)d;

    // do something with control.ExtractedData
}