Guest User

Untitled

a guest
Jan 24th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. <Page.Resources>
  2. <!--control style-->
  3. <Style TargetType="local:CustomLabel">
  4. <Setter Property="Template">
  5. <Setter.Value>
  6. <ControlTemplate TargetType="local:CustomLabel">
  7. <Grid>
  8. <Canvas>
  9. <Ellipse Height="10" Width="10" Canvas.Left="300" Canvas.Top="300" Fill="Red" />
  10. </Canvas>
  11. <Canvas>
  12. <ContentPresenter Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LabelMargin}" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
  13. </Canvas>
  14. </Grid>
  15. </ControlTemplate>
  16. </Setter.Value>
  17. </Setter>
  18. </Style>
  19. </Page.Resources>
  20. <Grid>
  21. <local:CustomLabel x:Name="label">
  22. <local:CustomLabel.Content>
  23. <Grid>
  24. <TextBlock Text="Initial Label" />
  25. </Grid>
  26. </local:CustomLabel.Content>
  27. </local:CustomLabel>
  28. <Button Content="Change text" VerticalAlignment="Bottom" Height="75" Click="Button_Click" />
  29. </Grid>
  30.  
  31. public MainPage()
  32. {
  33. this.InitializeComponent();
  34. }
  35.  
  36. private void Button_Click(object sender, RoutedEventArgs e)
  37. {
  38. var grid = new Grid();
  39. grid.Children.Add(new TextBlock() { Text = "Change" });
  40. this.label.Content = grid;
  41. }
  42.  
  43. // control implementation
  44. public class CustomLabel : Control
  45. {
  46. bool loadTime = false;
  47. public CustomLabel()
  48. {
  49. this.SizeChanged += CustomLabel_SizeChanged;
  50. }
  51. private void CustomLabel_SizeChanged(object sender, SizeChangedEventArgs e)
  52. {
  53. loadTime = true;
  54. CalculateContentPosition();
  55. }
  56. private void CalculateContentPosition()
  57. {
  58. if (loadTime)
  59. {
  60. var content = Content as FrameworkElement;
  61. if (content != null)
  62. {
  63. var left = content.ActualWidth / 2;
  64. var top = content.ActualHeight / 2;
  65. this.LabelMargin = new Thickness(300 - left, 300 - top, 0, 0);
  66. }
  67. }
  68. }
  69. public object Content
  70. {
  71. get { return (object)GetValue(ContentProperty); }
  72. set { SetValue(ContentProperty, value); }
  73. }
  74.  
  75. // Using a DependencyProperty as the backing store for Content. This enables animation, styling, binding, etc...
  76. public static readonly DependencyProperty ContentProperty =
  77. DependencyProperty.Register("Content", typeof(object), typeof(CustomLabel), new PropertyMetadata(null,OnContentChanged));
  78. private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  79. {
  80. (d as CustomLabel).CalculateContentPosition();
  81. }
  82. public Thickness LabelMargin
  83. {
  84. get { return (Thickness)GetValue(LabelMarginProperty); }
  85. set { SetValue(LabelMarginProperty, value); }
  86. }
  87. // Using a DependencyProperty as the backing store for LabelMargin. This enables animation, styling, binding, etc...
  88. public static readonly DependencyProperty LabelMarginProperty =
  89. DependencyProperty.Register("LabelMargin", typeof(Thickness), typeof(CustomLabel), new PropertyMetadata(new Thickness(1)));
  90. }
  91.  
  92. public CustomLabel()
  93. {
  94. this.SizeChanged += CustomLabel_SizeChanged;
  95. this.LayoutUpdated += CustomLabel_LayoutUpdated;
  96. }
  97.  
  98. private void CustomLabel_LayoutUpdated(object sender, object e)
  99. {
  100. CalculateContentPosition();
  101. }
Add Comment
Please, Sign In to add comment