Click here to Skip to main content
15,896,726 members
Articles / Desktop Programming / WPF

ListView, ComboBox, and ObservableCollection<T>

Rate me:
Please Sign up or sign in to vote.
4.36/5 (8 votes)
3 Feb 2010MIT5 min read 82.2K   4.9K   29  
An article on WPF data binding using ObservableCollection.
using System;
using System.Reflection;

namespace CoreMVVM.Messenger
{
    /// <summary>
    /// This class is an implementation detail of the MessageToActionsMap class.
    /// </summary>
    internal class WeakAction
    {
        readonly MethodInfo method;
        readonly Type delegateType;
        readonly WeakReference weakRef;

        /// <summary>
        /// Constructs a WeakAction
        /// </summary>
        /// <param name="target">The instance to be stored as a weak reference</param>
        /// <param name="method">The Method Info to create the action for</param>
        /// <param name="parameterType">The type of parameter to be passed in the action. Pass null if there is not a paramater</param>
        internal WeakAction(object target, MethodInfo method, Type parameterType)
        {
            //create a Weakefernce to store the instance of the target in which the Method resides
            weakRef = new WeakReference(target);

            this.method = method;

            // JAS - Added logic to construct callback type.
            if (parameterType == null)
                this.delegateType = typeof(Action);
            else
                this.delegateType = typeof(Action<>).MakeGenericType(parameterType);
        }

        /// <summary>
        /// Creates a "throw away" delegate to invoke the method on the target
        /// </summary>
        /// <returns></returns>
        internal Delegate CreateAction()
        {
            object target = weakRef.Target;
            if (target != null)
            {
                // Rehydrate into a real Action
                // object, so that the method
                // can be invoked on the target.
                return Delegate.CreateDelegate(
                            this.delegateType,
                            weakRef.Target,
                            method);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// returns true if the target is still in memory
        /// </summary>
        public bool IsAlive
        {
            get
            {
                return weakRef.IsAlive;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Technical Lead Rockwell Automation
Singapore Singapore
He is a Software Engineer at Rockwell Automation Asia Pacific Business Center, working on RSLogix 5000. Prior to joining Rockwell Automation, he had worked for Sybase for 8 years and was the original architect of the PowerBuilder Native Interface and the PowerBuilder .NET Compiler that can compile PowerBuilder applications to .NET Windows Forms or Web Forms applications. The programming languages he has used or is using intensively include C#, C++, C and 8086 assembly.

Wu XueSong's Blog

Comments and Discussions