Solved

Help with C# DependencyObject

Posted on 2012-03-15
3
596 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 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