Posted By

asim1985 on 06/05/13


Tagged

chathead


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

danfinrud55


CodeBehind ChatHead


 / Published in: C#
 

URL: http://asimmittal.net

Code behind my user control for Chathead

  1. namespace UserControls
  2. {
  3. public partial class ChatHead : UserControl
  4. {
  5. const double thickness = 1.5;
  6. public int Radius { get; set; }
  7. public string Label {
  8. get { return txtNotification.Text; }
  9. set { txtNotification.Text = value; }
  10. }
  11.  
  12. private string _imgPath;
  13. public string ImagePath {
  14. get {
  15. return _imgPath;
  16. }
  17. set{
  18. _imgPath = value;
  19. Uri uri = new Uri(_imgPath, UriKind.RelativeOrAbsolute);
  20. BitmapImage bmp = new BitmapImage(uri);
  21. image.Source = bmp;
  22. }
  23. }
  24.  
  25. /// <summary>
  26. /// Class constructor
  27. /// </summary>
  28. public ChatHead(){
  29. InitializeComponent();
  30. }
  31.  
  32. /// <summary>
  33. /// This creates the circular clip around this control
  34. /// </summary>
  35. private void invalidate() {
  36.  
  37. if (Radius >= 0 && Radius * 2 <= Width) {
  38.  
  39. //decide the center of the circular region
  40. double centerX = this.Height / 2;
  41. double centerY = this.Width / 2;
  42.  
  43. //create the clip geometry
  44. EllipseGeometry geom = new EllipseGeometry();
  45. geom.RadiusY = Radius - thickness;
  46. geom.RadiusX = Radius - thickness;
  47. geom.Center = new Point(centerX, centerY);
  48.  
  49. //apply the clip to this control
  50. image.Clip = geom;
  51.  
  52. //now setup the shape outline
  53. outline.Width = Radius * 2;
  54. outline.Height = Radius * 2;
  55.  
  56. //set the location of the notification
  57. double coord = (Width / 1.732) - Radius;
  58. notification.Margin = new Thickness(coord, coord, 0, 0);
  59. }
  60. }
  61.  
  62. /// <summary>
  63. /// Invoked when the control loads
  64. /// </summary>
  65. /// <param name="sender"></param>
  66. /// <param name="e"></param>
  67. private void UserControl_Loaded_1(object sender, RoutedEventArgs e) {
  68. invalidate();
  69. }
  70.  
  71. /// <summary>
  72. /// Invoked when thie control is resized
  73. /// </summary>
  74. /// <param name="sender"></param>
  75. /// <param name="e"></param>
  76. private void UserControl_SizeChanged_1(object sender, SizeChangedEventArgs e) {
  77. Height = Width;
  78. invalidate();
  79. }
  80. }
  81. }

Report this snippet  

You need to login to post a comment.