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

/src/net45/amarok.presentation/Presentation/Converter/Object/IndexToObjectConverter.cs

http://amarok.codeplex.com
C# | 119 lines | 56 code | 14 blank | 49 comment | 8 complexity | 260f92f8c39bf39e870e5cd5bac40852 MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause
  1. #region Copyright (c) 07/18/2012, Olaf Kober <amarok.projects@gmail.com>
  2. //================================================================================
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. //================================================================================
  21. #endregion
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Globalization;
  25. using System.Windows;
  26. using System.Windows.Data;
  27. using System.Windows.Markup;
  28. namespace Amarok.Presentation
  29. {
  30. /// <summary>
  31. /// Converts the given Int32 to the Object at the index position in the set Values collection.
  32. ///
  33. /// :: Convert
  34. /// 0..n ... Object at the Index position
  35. /// Out-Of-Range ... DefaultValue
  36. ///
  37. /// :: ConvertBack
  38. /// equal to Object at Index position ... Index
  39. /// not equal to any Object ... DependencyProperty.Unset
  40. /// </summary>
  41. [ContentProperty("Values")]
  42. [ValueConversion(typeof(Int32), typeof(Object))]
  43. public sealed class IndexToObjectConverter : ValueConverter<Int32, Object>
  44. {
  45. /// <summary>
  46. /// Gets or sets a collection of Objects. The Convert() method uses this collection to return the Object
  47. /// at the index that is represented by the supplied Int32 value. Defaults to an empty list.
  48. /// </summary>
  49. public List<Object> Values
  50. {
  51. get;
  52. set;
  53. }
  54. /// <summary>
  55. /// Gets or sets the Object that is returned by the Convert() method when the supplied Int32 value
  56. /// is outside of the list collection bounds. Defaults to DependencyProperty.UnsetValue.
  57. /// </summary>
  58. public Object DefaultValue
  59. {
  60. get;
  61. set;
  62. }
  63. /// <summary>
  64. /// Gets or sets the Object that is returned by the ConvertBack() method when the supplied Object is not
  65. /// equal to any of the Objects stored in the Values collection. Defaults to DependencyProperty.UnsetValue.
  66. /// </summary>
  67. public Object DefaultIndex
  68. {
  69. get;
  70. set;
  71. }
  72. /// <summary>
  73. /// Initializes a new instance.
  74. /// </summary>
  75. public IndexToObjectConverter()
  76. {
  77. this.Values = new List<Object>();
  78. this.DefaultValue = DependencyProperty.UnsetValue;
  79. this.DefaultIndex = DependencyProperty.UnsetValue;
  80. }
  81. /// <summary>
  82. /// </summary>
  83. protected override Object OnConvert(Int32 value, Type targetType, Object parameter, CultureInfo culture)
  84. {
  85. if (this.Values == null)
  86. return this.DefaultValue;
  87. if (value < 0 || value >= this.Values.Count)
  88. return this.DefaultValue;
  89. return this.Values[value];
  90. }
  91. /// <summary>
  92. /// </summary>
  93. protected override Object OnConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
  94. {
  95. if (this.Values == null)
  96. return this.DefaultIndex;
  97. for (Int32 i = 0; i < this.Values.Count; i++)
  98. {
  99. if (Object.Equals(this.Values[i], value))
  100. return i;
  101. }
  102. return this.DefaultIndex;
  103. }
  104. }
  105. }