Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help with C# DependencyObject

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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