PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/StockTrader RI/Silverlight/StockTraderRI.Infrastructure.Silverlight/DependencyPropertyHelper.cs

#
C# | 51 lines | 22 code | 3 blank | 26 comment | 4 complexity | 416bd0dd5f7b2531ab73656c14fdb744 MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System.Windows;
  18. using System;
  19. namespace StockTraderRI.Infrastructure
  20. {
  21. /// <summary>
  22. /// Defines helper methods for dependency properties.
  23. /// </summary>
  24. public static class DependencyPropertyHelper
  25. {
  26. /// <summary>
  27. /// Gets or, if it is not defined sets, the <paramref name="property"/> value on the <paramref name="dependencyObject"/> object.
  28. /// </summary>
  29. /// <typeparam name="T"><see cref="System.Type"/> of the <see cref="DependencyProperty"/>.</typeparam>
  30. /// <param name="dependencyObject">Object on which the <paramref name="property"/> is set.</param>
  31. /// <param name="property">Property whose value will be retrieved.</param>
  32. /// <returns>Value of <paramref name="property"/> on <paramref name="dependencyObject"/>.</returns>
  33. public static T GetOrAddValue<T>(DependencyObject dependencyObject, DependencyProperty property) where T : class, new()
  34. {
  35. if (dependencyObject == null)
  36. {
  37. throw new ArgumentNullException("dependencyObject");
  38. }
  39. T value = dependencyObject.GetValue(property) as T;
  40. if (value == null)
  41. {
  42. value = new T();
  43. dependencyObject.SetValue(property, value);
  44. }
  45. return value;
  46. }
  47. }
  48. }