ImageView.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Media;
  10. namespace FWindSoft.Wpf.Controls
  11. {
  12. public class ImageView : ViewBase
  13. {
  14. #region 定义传递依赖参数
  15. public static readonly DependencyProperty ImageProperty =
  16. DependencyProperty.RegisterAttached("Image", typeof(ImageSource), typeof(ImageView));
  17. [AttachedPropertyBrowsableForType(typeof(ListViewItem))]
  18. public static ImageSource GetImage(DependencyObject dp)
  19. {
  20. return dp.GetValue(ImageProperty) as ImageSource;
  21. }
  22. [AttachedPropertyBrowsableForType(typeof(ListViewItem))]
  23. public static void SetImage(DependencyObject dp, object value)
  24. {
  25. dp.SetValue(ImageProperty, value);
  26. }
  27. public static readonly DependencyProperty DisplayProperty =
  28. DependencyProperty.RegisterAttached("Display", typeof(string), typeof(ImageView));
  29. [AttachedPropertyBrowsableForType(typeof(ListViewItem))]
  30. public static string GetDisplay(DependencyObject dp)
  31. {
  32. return dp.GetValue(DisplayProperty) as string;
  33. }
  34. [AttachedPropertyBrowsableForType(typeof(ListViewItem))]
  35. public static void SetDisplay(DependencyObject dp, string value)
  36. {
  37. dp.SetValue(DisplayProperty, value);
  38. }
  39. public static readonly DependencyProperty ToolTipProperty =
  40. DependencyProperty.RegisterAttached("ToolTip", typeof(string), typeof(ImageView));
  41. [AttachedPropertyBrowsableForType(typeof(ListViewItem))]
  42. public static string GetToolTip(DependencyObject dp)
  43. {
  44. return dp.GetValue(ToolTipProperty) as string;
  45. }
  46. [AttachedPropertyBrowsableForType(typeof(ListViewItem))]
  47. public static void SetToolTip(DependencyObject dp, string value)
  48. {
  49. dp.SetValue(ToolTipProperty, value);
  50. }
  51. #endregion
  52. protected override object DefaultStyleKey => new ComponentResourceKey(GetType(), "DefaultStyle");
  53. protected override object ItemContainerDefaultStyleKey => new ComponentResourceKey(GetType(), "ItemContainerDefaultStyle");
  54. protected override void PrepareItem(ListViewItem item)
  55. {
  56. if(item.DataContext==null)
  57. {
  58. item.DataContext = item;
  59. }
  60. if (ImageBinding != null)
  61. {
  62. item.SetBinding(ImageProperty, ImageBinding);
  63. }
  64. if (DisplayBinding != null)
  65. {
  66. item.SetBinding(DisplayProperty, DisplayBinding);
  67. }
  68. if (ToolTipBinding != null)
  69. {
  70. item.SetBinding(ToolTipProperty, ToolTipBinding);
  71. }
  72. }
  73. /// <summary>
  74. /// 显示信息绑定
  75. /// </summary>
  76. public BindingBase DisplayBinding { get; set; }
  77. /// <summary>
  78. /// ToopTip绑定
  79. /// </summary>
  80. public BindingBase ToolTipBinding { get; set; }
  81. /// <summary>
  82. /// 图片绑定
  83. /// </summary>
  84. public BindingBase ImageBinding { get; set; }
  85. public static readonly DependencyProperty ItemWidthProperty =
  86. WrapPanel.ItemWidthProperty.AddOwner(typeof(ImageView),new PropertyMetadata(120d));
  87. /// <summary>
  88. /// 宽
  89. /// </summary>
  90. public double ItemWidth
  91. {
  92. get { return (double)GetValue(ItemWidthProperty); }
  93. set { SetValue(ItemWidthProperty, value); }
  94. }
  95. public static readonly DependencyProperty ItemHeightProperty =
  96. WrapPanel.ItemHeightProperty.AddOwner(typeof(ImageView), new PropertyMetadata(120d));
  97. /// <summary>
  98. /// 项目高
  99. /// </summary>
  100. public double ItemHeight
  101. {
  102. get { return (double)GetValue(ItemHeightProperty); }
  103. set { SetValue(ItemHeightProperty, value); }
  104. }
  105. }
  106. }