?
Solved

Help with C# DependencyObject

Posted on 2012-03-15
3
Medium Priority
?
616 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
[X]
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
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

762 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