Lorem Ipsum Attached Property – C# Code

    public class LoremIpsumInteractor : DependencyObject, ILoremIpsum
    {
        #region DependencyProperties

        #region TotalSentences

        public static readonly DependencyProperty TotalSentencesProperty =
            DependencyProperty.RegisterAttached("TotalSentences", typeof (int), typeof (LoremIpsumInteractor),
                                                new UIPropertyMetadata(1, OnTotalSentencesPropertyChange));

        public static int GetTotalSentences(DependencyObject obj)
        {
            return (int) obj.GetValue(TotalSentencesProperty);
        }

        public static void SetTotalSentences(DependencyObject obj, int value)
        {
            obj.SetValue(TotalSentencesProperty, value);
        }

        private static void OnTotalSentencesPropertyChange(DependencyObject source,
                                                           DependencyPropertyChangedEventArgs args)
        {
            if (!(source is TextBox))
            {
                return; // support only a TextBox
            }

            if (!(args.NewValue is int))
            {
                return; // use integer values
            }
        }

        #endregion

        #region TotalWords

        public static readonly DependencyProperty TotalWordsProperty =
            DependencyProperty.RegisterAttached("TotalWords", typeof (int), typeof (LoremIpsumInteractor),
                                                new UIPropertyMetadata(0, OnTotalWordsPropertyChange));

        private static void OnTotalWordsPropertyChange(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            if (!(source is TextBox))
            {
                return; // support only a TextBox
            }

            if (!(args.NewValue is int))
            {
                return; // use integer values
            }
        }

        public static int GetTotalWords(DependencyObject obj)
        {
            return (int) obj.GetValue(TotalWordsProperty);
        }

        public static void SetTotalWords(DependencyObject obj, int value)
        {
            obj.SetValue(TotalWordsProperty, value);
        }

        #endregion

        #region TotalParagraphs

        public static readonly DependencyProperty TotalParagraphsProperty =
            DependencyProperty.RegisterAttached("TotalParagraphs", typeof (int),
                                                typeof (LoremIpsumInteractor),
                                                new UIPropertyMetadata(1, OnTotalParagraphsChange));

        public static int GetTotalParagraphs(DependencyObject obj)
        {
            return (int) obj.GetValue(TotalParagraphsProperty);
        }

        public static void SetTotalParagraphs(DependencyObject obj, int value)
        {
            obj.SetValue(TotalParagraphsProperty, value);
        }

        private static void OnTotalParagraphsChange(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            DependencyProperty prop = args.Property;
            prop.ToString();
        }

        #endregion

        #region Process

        public static readonly DependencyProperty ProcessProperty =
            DependencyProperty.RegisterAttached("Process", typeof (bool), typeof (LoremIpsumInteractor),
                                                new UIPropertyMetadata(false, OnProcessChange));

        private static void OnProcessChange(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            // check to see what values we have accumulated until now
            if (!(source is TextBox))
            {
                return; // support only a TextBox
            }

            if (!(args.NewValue is bool))
            {
                return; // use bool values
            }

            var val = (bool)args.NewValue;
            var textBox = source as TextBox;

            if (!val) return;
            var sg = new LoremIpsumStringGenerator();
            textBox.Text = sg.GetTextBody(25, 4, 3);
        }

        public static bool GetProcess(DependencyObject obj)
        {
            return (bool) obj.GetValue(ProcessProperty);
        }

        public static void SetProcess(DependencyObject obj, bool value)
        {
            obj.SetValue(ProcessProperty, value);
        }

        #endregion

        #endregion

        /// <summary>
        /// Invoked whenever the effective value of any dependency property on 
        /// this <see cref="T:System.Windows.DependencyObject"/> has been updated. 
        /// The specific dependency property that changed is reported in the event data.
        /// </summary>
        /// <param name="args">Event data that will contain the dependency property identifier 
        /// of interest, the property metadata for the type, and old and new values.</param>
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
        {
            DependencyProperty prop = args.Property;
            System.Diagnostics.Debug.WriteLine( "...." + prop);
        }
    }

    /// <summary>
    /// Lorem Ipsum generator class for C#
    /// </summary>
    internal sealed class LoremIpsumStringGenerator
    {
        private readonly string[] words = new[] { "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod",
    "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua",
    "at", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "stet", "clita",
    "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "lorem", "ipsum", "dolor", "sit", "amet",
    "lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod",
    "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua",
    "at", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "stet", "clita",
    "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "lorem", "ipsum", "dolor", "sit", "amet",
    "lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod",
    "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua",
    "at", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "stet", "clita",
    "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "lorem", "ipsum", "dolor", "sit", "amet", "duis",
    "autem", "vel", "eum", "iriure", "dolor", "in", "hendrerit", "in", "vulputate", "velit", "esse", "molestie",
    "consequat", "vel", "illum", "dolore", "eu", "feugiat", "nulla", "facilisis", "at", "vero", "eros", "et",
    "accumsan", "et", "iusto", "odio", "dignissim", "qui", "blandit", "praesent", "luptatum", "zzril", "delenit",
    "augue", "duis", "dolore", "te", "feugait", "nulla", "facilisi", "lorem", "ipsum", "dolor", "sit", "amet",
    "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet",
    "dolore", "magna", "aliquam", "erat", "volutpat", "ut", "wisi", "enim", "ad", "minim", "veniam", "quis",
    "nostrud", "exerci", "tation", "ullamcorper", "suscipit", "lobortis", "nisl", "ut", "aliquip", "ex", "ea",
    "commodo", "consequat", "duis", "autem", "vel", "eum", "iriure", "dolor", "in", "hendrerit", "in", "vulputate",
    "velit", "esse", "molestie", "consequat", "vel", "illum", "dolore", "eu", "feugiat", "nulla", "facilisis", "at",
    "vero", "eros", "et", "accumsan", "et", "iusto", "odio", "dignissim", "qui", "blandit", "praesent", "luptatum",
    "zzril", "delenit", "augue", "duis", "dolore", "te", "feugait", "nulla", "facilisi", "nam", "liber", "tempor",
    "cum", "soluta", "nobis", "eleifend", "option", "congue", "nihil", "imperdiet", "doming", "id", "quod", "mazim",
    "placerat", "facer", "possim", "assum", "lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing",
    "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam",
    "erat", "volutpat", "ut", "wisi", "enim", "ad", "minim", "veniam", "quis", "nostrud", "exerci", "tation",
    "ullamcorper", "suscipit", "lobortis", "nisl", "ut", "aliquip", "ex", "ea", "commodo", "consequat", "duis",
    "autem", "vel", "eum", "iriure", "dolor", "in", "hendrerit", "in", "vulputate", "velit", "esse", "molestie",
    "consequat", "vel", "illum", "dolore", "eu", "feugiat", "nulla", "facilisis", "at", "vero", "eos", "et", "accusam",
    "et", "justo", "duo", "dolores", "et", "ea", "rebum", "stet", "clita", "kasd", "gubergren", "no", "sea",
    "takimata", "sanctus", "est", "lorem", "ipsum", "dolor", "sit", "amet", "lorem", "ipsum", "dolor", "sit",
    "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut",
    "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua", "at", "vero", "eos", "et",
    "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "stet", "clita", "kasd", "gubergren", "no",
    "sea", "takimata", "sanctus", "est", "lorem", "ipsum", "dolor", "sit", "amet", "lorem", "ipsum", "dolor", "sit",
    "amet", "consetetur", "sadipscing", "elitr", "at", "accusam", "aliquyam", "diam", "diam", "dolore", "dolores",
    "duo", "eirmod", "eos", "erat", "et", "nonumy", "sed", "tempor", "et", "et", "invidunt", "justo", "labore",
    "stet", "clita", "ea", "et", "gubergren", "kasd", "magna", "no", "rebum", "sanctus", "sea", "sed", "takimata",
    "ut", "vero", "voluptua", "est", "lorem", "ipsum", "dolor", "sit", "amet", "lorem", "ipsum", "dolor", "sit",
    "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut",
    "labore", "et", "dolore", "magna", "aliquyam", "erat", "consetetur", "sadipscing", "elitr", "sed", "diam",
    "nonumy", "eirmod", "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed",
    "diam", "voluptua", "at", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea",
    "rebum", "stet", "clita", "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "lorem", "ipsum" };

        private int Randomise()
        {
            var str = DateTime.Now.Millisecond.ToString();
            if (str.Length == 1)
                str = " " + str;        // pad out
            if (str.Length == 2)
                str = " " + str;        // pad out
            var seed = Convert.ToInt32(str.Substring(1, 2));
            var cnr = new Random(seed).Next(words.Length - 1);
            // too short between calls otherwise - would return same value next call
            System.Threading.Thread.Sleep(1);   
            return cnr;
        }

        /// <summary>
        /// Rreturns a string filled with LoremIpsun text.
        /// </summary>
        /// <param name="numWords">The num words.</param>
        /// <returns></returns>
        public string GetWords(int numWords)
        {
            var result = new StringBuilder();
            result.Append("Lorem ipsum dolor sit amet");

            var random = new Random();

            for (var i = 0; i <= numWords; i++)
            {
                result.Append(" " + words[Randomise()]);
            }

            result.Append(".");
            return result.ToString();
        }

        public string GetNewWords(int numWords)
        {
            var result = new StringBuilder();
            var firstWord = true;
            var random = new Random();

            for (var i = 0; i <= numWords; i++)
            {
                if ( firstWord )
                {
                    var wrd = words[Randomise()];
                    var arr = wrd.ToCharArray();
                    var ch=char.ToUpper(wrd[0]);
                    arr[0] = ch;
                    result.Append(arr);
                    firstWord = false;
                }
                else
                {
                    result.Append(" " + words[Randomise()]);
                }
            }

            result.Append(".");
            return result.ToString();
        }

        public string GetWords(int numWords, int lowerBound, int upperBound)
        {
            var result = new StringBuilder();
            result.Append("Lorem ipsum dolor sit amet");

            var random = new Random();

            for (var i = 0; i <= numWords; i++)
            {
                result.Append(" " + words[random.Next(words.Length - 1)]);
            }

            result.Append(".");
            return result.ToString();
        }

        public string GetSentence(int numWords, int numSentences)
        {
            var result = new StringBuilder();

            result.Append(this.GetWords(numWords) + " ");
            for (var i = 0; i <= numSentences-1; i++)
            {
                result.Append(this.GetNewWords(numWords) + " ");
            }

            return result.ToString();
        }

        public string GetTextBody( int numWords, int numSentences, int numParagraphs )
        {
            var result = new StringBuilder();

            for (var i = 0; i <= numParagraphs; i++)
            {
                result.Append(this.GetSentence(numWords, numSentences) + Environment.NewLine + Environment.NewLine);
            }

            return result.ToString();
        }
    }

2 Responses to “Lorem Ipsum Attached Property – C# Code”

  1. […] code can be found here, and the XAML here. Below is an example of the Lorum Ipsum attached property in use. […]

  2. […] code can be found here, and the XAML here. Below is an example of the Lorum Ipsum attached property in use. […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: