Link to home
Start Free TrialLog in
Avatar of locdang
locdang

asked on

Help with C# DependencyObject

Hi Everyone,

I have a custom control (WPF) that has a couple dependancy objects in it, when the value of any one of these DP objects are updated I need it to run a method.

However;

Because the object's DPs are databound in XAML when used the call to the method i want to run on DP value change isn't called as the DP is access directly and not through the obj property.

How can i trigger the same way from a XAML value assignment to a DP?

Thanks,

Xavier.



 public class CDNImage : Control
    {
        public enum CDNFetchModes
        {
            GetLatestFromMediaStorageLocation,
            GetSpecificMediaItemVersion
        }

        public void ReloadImage()
        {
            if (CDNFetchMode == CDNFetchModes.GetLatestFromMediaStorageLocation)
            {
                if (MediaStorageLocationID != null && MediaStorageLocationID != new Guid())
                {
                    var r = Epicentre.ContentDeliveryNetwork.CDNController.GetLatestVersionOfMedia(MediaStorageLocationID);
                    if (Epicentre.Types.IO.MIMEType.Parse(r.MediaItemVersionInfo.MIMEType).Type == "image")
                    {
                        MemoryStream byteStream = new MemoryStream(r.MediaItemVersionInfo.FileData);
                        BitmapImage image = new BitmapImage();
                        image.BeginInit();
                        image.StreamSource = byteStream;
                        image.EndInit();
                        ImageData = image;
                    }
                    else
                    {
                        ImageData = new BitmapImage(new Uri("/OSManager;component/Icons/64/Gnome-Document-New-64.png", UriKind.RelativeOrAbsolute));
                    }
                }
                else
                {
                    ImageData = new BitmapImage(new Uri("/OSManager;component/no-item-selected.png", UriKind.RelativeOrAbsolute));
                }
            }
            else
            {
                if (MediaStorageLocationID != null && MediaItemVersionID != null && MediaStorageLocationID != new Guid() && MediaItemVersionID != new Guid())
                {
                    var r = Epicentre.ContentDeliveryNetwork.CDNController.GetSpecificVersionOfMedia(MediaStorageLocationID, MediaItemVersionID);
                    if (Epicentre.Types.IO.MIMEType.Parse(r.MediaItemVersionInfo.MIMEType).Type == "image")
                    {
                        MemoryStream byteStream = new MemoryStream(r.MediaItemVersionInfo.FileData);
                        BitmapImage image = new BitmapImage();
                        image.BeginInit();
                        image.StreamSource = byteStream;
                        image.EndInit();
                        ImageData = image;
                    }
                    else
                    {
                        ImageData = new BitmapImage(new Uri("/OSManager;component/Icons/64/Gnome-Document-New-64.png", UriKind.RelativeOrAbsolute));
                    }
                }
                else
                {
                    ImageData = new BitmapImage(new Uri("/OSManager;component/no-item-selected.png", UriKind.RelativeOrAbsolute));
                }
            }
        }


        private static void vChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            
        }

        public Guid MediaStorageLocationID
        {
            get { return (Guid)GetValue(MediaStorageLocationIDProperty); }
            set { SetValue(MediaStorageLocationIDProperty, value); ReloadImage(); }
        }

        // Using a DependencyProperty as the backing store for MediaStorageLocationID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MediaStorageLocationIDProperty =
            DependencyProperty.Register("MediaStorageLocationID", typeof(Guid), typeof(CDNImage), new UIPropertyMetadata(null));



        public Guid MediaItemVersionID
        {
            get { return (Guid)GetValue(MediaItemVersionIDProperty); }
            set { SetValue(MediaItemVersionIDProperty, value); ReloadImage(); }
        }

        // Using a DependencyProperty as the backing store for MediaItemVersionID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MediaItemVersionIDProperty =
            DependencyProperty.Register("MediaItemVersionID", typeof(Guid), typeof(CDNImage), new UIPropertyMetadata(null));



        public CDNFetchModes CDNFetchMode
        {
            get { return (CDNFetchModes)GetValue(CDNFetchModeProperty); }
            set { SetValue(CDNFetchModeProperty, value); ReloadImage(); }
        }

        // Using a DependencyProperty as the backing store for CDNFetchMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CDNFetchModeProperty =
            DependencyProperty.Register("CDNFetchMode", typeof(CDNFetchModes), typeof(CDNImage), new UIPropertyMetadata(CDNFetchModes.GetLatestFromMediaStorageLocation));



        public BitmapImage ImageData
        {
            get { return (BitmapImage)GetValue(ImageDataProperty); }
            set { SetValue(ImageDataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageData.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageDataProperty =
            DependencyProperty.Register("ImageData", typeof(BitmapImage), typeof(CDNImage), new UIPropertyMetadata(null));


        public CDNImage()
        {
            ReloadImage();
        }

        static CDNImage()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CDNImage), new FrameworkPropertyMetadata(typeof(CDNImage)));
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of locdang
locdang

ASKER

The calls to ReloadImage on Lines 70, 82, 94 are not executed when the value is changed via a XAML binding, if you in the code behind change the value of these properties then the calls are fired.
Avatar of locdang

ASKER

Thanks, you actually solved the issue for me :)

I just needed to impliment the DependencyPropertyChanged event and boom im done.

Thank you!

Xavier.