Click here to Skip to main content
15,891,473 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.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

namespace Petzold.Media3D
{
    // Derive from TransformInfo so can define ZeroMatrix and Name properties ????

    public class VisualInfo : Animatable
    {
        public static readonly Matrix3D ZeroMatrix = new Matrix3D(0, 0, 0, 0, 0, 0, 0, 0,
                                                           0, 0, 0, 0, 0, 0, 0, 0);


        public static readonly DependencyProperty ModelVisual3DProperty =
            DependencyProperty.Register("ModelVisual3D",
                typeof(ModelVisual3D),
                typeof(VisualInfo),
                new PropertyMetadata(null, ModelVisual3DChanged));

        public ModelVisual3D ModelVisual3D
        {
            set { SetValue(ModelVisual3DProperty, value); }
            get { return (ModelVisual3D)GetValue(ModelVisual3DProperty); }
        }

        static readonly DependencyPropertyKey TotalTransformKey =
            DependencyProperty.RegisterReadOnly("TotalTransform",
                typeof(Matrix3D),
                typeof(VisualInfo),
                new PropertyMetadata(new Matrix3D()));

        public static readonly DependencyProperty TotalTransformProperty =
            TotalTransformKey.DependencyProperty;

        public Matrix3D TotalTransform
        {
            protected set { SetValue(TotalTransformKey, value); }
            get { return (Matrix3D)GetValue(TotalTransformProperty); }
        }


        static void ModelVisual3DChanged(DependencyObject obj, 
                                         DependencyPropertyChangedEventArgs args)
        {
            (obj as VisualInfo).ModelVisual3DChanged(args);
        }

        void ModelVisual3DChanged(DependencyPropertyChangedEventArgs args)
        {
            TotalTransform = GetTotalTransform(args.NewValue as ModelVisual3D);
        }



        // TotalTransform

        // ViewportTransoform

        // SpaceTransform




                                // could have Model3D here as well????
                                // But can't find group that it's a part of.
                                // Plus, it's not unique since it can be shared !!!!


        public static Matrix3D GetTotalTransform(DependencyObject obj)      // argument is really a ModelVisual3D
        {
            Matrix3D matx = Matrix3D.Identity;

            while (!(obj is Viewport3DVisual))
            {
                // This occurs when the visual is parent-less.
                if (obj == null)
                {
                    return ZeroMatrix;
                }

                else if (obj is ModelVisual3D)
                {
                    if ((obj as ModelVisual3D).Transform != null)
                        matx.Append((obj as ModelVisual3D).Transform.Value);
                }

                else
                {
                    throw new ApplicationException("didn't end in Viewport3DVisual");
                }

                obj = VisualTreeHelper.GetParent(obj);
            }

            // At this point, we know obj is Viewport3DVisual
            Viewport3DVisual vis = obj as Viewport3DVisual;
            Matrix3D matxViewport = ViewportInfo.GetTotalTransform(vis);
            matx.Append(matxViewport);

            return matx;
        }

        public override string ToString()
        {
            return TotalTransform.ToString();
        }

        protected override Freezable CreateInstanceCore()
        {
            return new VisualInfo();
        }
    }
}

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