Solved

Help with C# DependencyObject

Posted on 2012-03-15
3
587 Views
Last Modified: 2012-03-19
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

0
Comment
Question by:locdang
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 37737150
i didnt get u, even if the dependency property changes thru code or thru xaml the DependencyPropertyChanged methd shud fire. Am i missing something ?
0
 
LVL 1

Author Comment

by:locdang
ID: 37739977
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.
0
 
LVL 1

Author Closing Comment

by:locdang
ID: 37740095
Thanks, you actually solved the issue for me :)

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

Thank you!

Xavier.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now