Click here to Skip to main content
15,886,872 members
Articles / Web Development / ASP.NET

Take MVC to the Next Level in .NET

Rate me:
Please Sign up or sign in to vote.
4.62/5 (11 votes)
30 Apr 2013GPL315 min read 73.1K   858   75  
How to quickly build reusable and flexible WPF, Silverlight, or ASP.NET applications with the powerful Xomega Framework using the best MVVM principles.
// Copyright (c) 2010-2012 Xomega.Net. All rights reserved.

using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace Xomega.Framework
{
    /// <summary>
    /// A generic binding that allows displaying property items (possible values)
    /// formatted by the data property according to the specified value format
    /// (typically <see cref="ValueFormat.DisplayString"/>) based on the property configuration.
    /// For example, if property values and possible items are of type <c>Header</c>
    /// and the property is configured to display its values as a combination of ID and Name,
    /// then the list of possible items (and not just the property value) will be also displayed like this.
    /// This class is used internally by the <see cref="Binding.SelectorPropertyBinding"/> class
    /// but can also be used in the XAML by application developers when a custom list item template
    /// is being used.
    /// </summary>
    /// <example>
    /// Below is the default list item data template used by Xomega framework. Developers can enhance it
    /// in XAML to provide a more sophisticated template for list items.
    /// <DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    /// xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    /// xmlns:xom='clr-namespace:Xomega.Framework;assembly=Xomega.Framework'>
    /// <TextBlock xom:DataPropertyItemBinding.ValueFormat='{x:Static xom:ValueFormat.DisplayString}'/></DataTemplate>
    /// </example>
    /// <remarks>Note: This code doesn't work in Silverlight 3 due to its limitations to obtain combo box from the item.</remarks>
    public class DataPropertyItemBinding
    {
        /// <summary>
        /// Gets the value format dependency property that is set on the given text block if any.
        /// </summary>
        /// <param name="obj">The text block element.</param>
        /// <returns>The value format dependency property that is set on the given text block if any.</returns>
        public static ValueFormat GetValueFormat(TextBlock obj)
        {
            return (ValueFormat)obj.GetValue(ValueFormatProperty);
        }

        /// <summary>
        /// Sets the value format dependency property on a text block.
        /// </summary>
        /// <param name="obj">The text block element to set the property on.</param>
        /// <param name="value">The value format to set on the text block.</param>
        public static void SetValueFormat(TextBlock obj, ValueFormat value)
        {
            obj.SetValue(ValueFormatProperty, value);
        }

        /// <summary>
        /// A ValueFormat dependency property that needs to be set on a TextBlock inside a list item template
        /// to make it display the string for the current item formatted according to the property's rules.
        /// </summary>
        public static readonly DependencyProperty ValueFormatProperty = DependencyProperty.RegisterAttached(
            "Bind", typeof(ValueFormat), typeof(DataPropertyItemBinding), new PropertyMetadata(OnValueFormatChanged));

        /// <summary>
        /// A callback that is triggered when a value format dependency properties is set.
        /// </summary>
        /// <param name="d">The dependency object the property has been set on.</param>
        /// <param name="e">Event arguments.</param>
        public static void OnValueFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBlock el = d as TextBlock;
            if (el == null) return;

            OnDataContextChanged(el, new DependencyPropertyChangedEventArgs());
#if !SILVERLIGHT
            el.DataContextChanged += OnDataContextChanged;
#endif
        }

        /// <summary>
        /// A handler of the data context change event on a text block that has a value format dependency property set,
        /// which updates the text of the text block to be formatted according to the specified value format.
        /// </summary>
        /// <param name="sender">Event sender, which should be a text block.</param>
        /// <param name="e">Event arguments.</param>
        private static void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            if (tb == null) return;

            DataPropertyBinding binding = GetBinding(tb);
            if (binding != null && binding.BoundProperty != null)
                tb.Text = binding.BoundProperty.ValueToString(tb.DataContext, GetValueFormat(tb));
        }

        /// <summary>
        /// Gets the data property binding from a dependency object or its parent.
        /// </summary>
        /// <param name="obj">Dependency object.</param>
        /// <returns>Data property binding or null if not found.</returns>
        private static DataPropertyBinding GetBinding(DependencyObject obj)
        {
            if (obj == null) return null;
            DataPropertyBinding b = obj.GetValue(Property.BindingProperty) as DataPropertyBinding;
            return b;
            // The following could've worked in Silverlight if the TemplatedParent wasn't internal
            //if (b != null) return b;
            //ItemsPresenter ip = obj as ItemsPresenter;
            //if (ip != null) return GetBinding(ip.TemplatedParent);
            //return GetBinding(VisualTreeHelper.GetParent(obj));
        }
    }
}

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 GNU General Public License (GPLv3)


Written By
Architect Xomega.Net
United States United States
Xomega Team is striving to increase productivity and development quality by utilizing Model Driven Development coupled with Code Generation and the best design practices for application development.
We provide MDD tools, code generators and frameworks for Visual Studio and .Net development.
Visit us at http://www.xomega.net
This is a Organisation

1 members

Comments and Discussions