DisableBorder.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace FWindSoft.Wpf.Controls
  6. {
  7. public class DisableBorder : Border
  8. {
  9. //public static readonly DependencyProperty DisableProperty = DependencyProperty.Register("Disable", typeof (bool),
  10. // typeof (DisableBorder), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  11. public static readonly DependencyProperty DisableBrushProperty = DependencyProperty.Register("DisableBrush", typeof(Brush),
  12. typeof(DisableBorder), new FrameworkPropertyMetadata(Brushes.Red, FrameworkPropertyMetadataOptions.AffectsRender));
  13. public static readonly DependencyProperty DisableThicknessProperty = DependencyProperty.Register("DisableThickness", typeof(double),
  14. typeof(DisableBorder), new FrameworkPropertyMetadata(5d, FrameworkPropertyMetadataOptions.AffectsRender));
  15. static DisableBorder()
  16. {
  17. IsEnabledProperty.OverrideMetadata(typeof(DisableBorder), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  18. }
  19. #region 包装属性
  20. //public bool Disable
  21. //{
  22. // set { SetValue(DisableProperty, value); }
  23. // get { return (bool)GetValue(DisableProperty); }
  24. //}
  25. public Brush DisableBrush
  26. {
  27. set { SetValue(DisableBrushProperty, value); }
  28. get { return (Brush)GetValue(DisableBrushProperty); }
  29. }
  30. public double DisableThickness
  31. {
  32. set { SetValue(DisableThicknessProperty, value); }
  33. get { return (double)GetValue(DisableThicknessProperty); }
  34. }
  35. #endregion
  36. protected override void OnRender(DrawingContext dc)
  37. {
  38. UIElement child = this.Child;
  39. double opacity = this.Opacity;
  40. if (!IsEnabled)
  41. {
  42. Point point1 = new Point(0, ActualHeight);
  43. Point point2 = new Point(ActualWidth, 0);
  44. dc.DrawLine(new Pen(DisableBrush, DisableThickness), point1, point2);
  45. opacity = opacity/2;
  46. }
  47. if(child!=null)
  48. child.Opacity = opacity;
  49. base.OnRender(dc);
  50. }
  51. }
  52. }