ClipClapDisplay Binding Support

Apr 23, 2010 at 6:20 AM
Edited Apr 23, 2010 at 6:20 AM

I have not used the other controls yet but this one sure ROCKS! Thanks for providing this to the community!

I modified the ClipClapDisplay control to support binding on the Text property. This works out very well with the new Silverlight 4 binding syntax that allows for StringFormat so that you can have a leading zero while bound to an numeric property.

Here is the code if you want to use it:

 

// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Effects;
using System;

namespace TheOliver.Controls
{
    public partial class ClipClapDisplay : UserControl
    {
        public ClipClapDisplay()
        {
            InitializeComponent();           
        }

        private double _characterMargin = 4;
        public double CharacterMargin
        {
            get { return _characterMargin; }
            set { _characterMargin = value; }
        }

             // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(ClipClapDisplay),
            new PropertyMetadata(new PropertyChangedCallback(SetNewValue)));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }


        private static void SetNewValue(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {

            ClipClapDisplay ctrl = (ClipClapDisplay)sender;
            ctrl.SetNewText(e.NewValue.ToString());

        }


        private void SetNewText(string value)
        {
            if (value == null)
            {
                return;
            }

            _helper.Text = value;

            for (int i = 0; i < value.Length; i++)
            {
                // Does character position exist
                ClipClapChar ccc;

                if (_characterStack.Children.Count > i)
                {
                    ccc = _characterStack.Children[i] as ClipClapChar;
                }
                else
                {
                    ccc = new ClipClapChar();
                    ccc.Margin = new System.Windows.Thickness(_characterMargin);

                    // Drop Shadow
                    DropShadowEffect dse = new DropShadowEffect();
                    ccc.Effect = dse;

                    _characterStack.Children.Add(ccc);
                }
                ccc.Character = value.Substring(i, 1);
            }

            // remove characters
            while (_characterStack.Children.Count > value.Length)
            {
                _characterStack.Children.RemoveAt(_characterStack.Children.Count - 1);
            }


        }
    }
}