How to Expose a DependencyProperty of a Control nested in a UserControl?

前端 未结 1 1873
迷失自我
迷失自我 2021-01-05 20:06

I\'m trying to bind an Image down from a Window into a UserControl \'Display\' thats inside a UserControl \'DisplayHandler\'. Display has a DependancyProperty \'DisplayImage

相关标签:
1条回答
  • 2021-01-05 20:47

    The AddOwner solution should be working, but you have to add a PropertyChangedCallback that updates the embedded control.

    public partial class DisplayHandler : UserControl
    {
        public static readonly DependencyProperty DisplayImageProperty =
            Display.DisplayImageProperty.AddOwner(typeof(DisplayHandler),
                new FrameworkPropertyMetadata(DisplayImagePropertyChanged));
    
        public HImage DisplayImage
        {
            get { return (Image)GetValue(DisplayImageProperty); }
            set { SetValue(DisplayImageProperty, value); }
        }
    
        private static void DisplayImagePropertyChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var dh = obj as DisplayHandler;
            dh.display1.DisplayImage = e.NewValue as HImage;
        }
    }
    
    0 讨论(0)
提交回复
热议问题