Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Page.Resources>
- <!--control style-->
- <Style TargetType="local:CustomLabel">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="local:CustomLabel">
- <Grid>
- <Canvas>
- <Ellipse Height="10" Width="10" Canvas.Left="300" Canvas.Top="300" Fill="Red" />
- </Canvas>
- <Canvas>
- <ContentPresenter Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LabelMargin}" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
- </Canvas>
- </Grid>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </Page.Resources>
- <Grid>
- <local:CustomLabel x:Name="label">
- <local:CustomLabel.Content>
- <Grid>
- <TextBlock Text="Initial Label" />
- </Grid>
- </local:CustomLabel.Content>
- </local:CustomLabel>
- <Button Content="Change text" VerticalAlignment="Bottom" Height="75" Click="Button_Click" />
- </Grid>
- public MainPage()
- {
- this.InitializeComponent();
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- var grid = new Grid();
- grid.Children.Add(new TextBlock() { Text = "Change" });
- this.label.Content = grid;
- }
- // control implementation
- public class CustomLabel : Control
- {
- bool loadTime = false;
- public CustomLabel()
- {
- this.SizeChanged += CustomLabel_SizeChanged;
- }
- private void CustomLabel_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- loadTime = true;
- CalculateContentPosition();
- }
- private void CalculateContentPosition()
- {
- if (loadTime)
- {
- var content = Content as FrameworkElement;
- if (content != null)
- {
- var left = content.ActualWidth / 2;
- var top = content.ActualHeight / 2;
- this.LabelMargin = new Thickness(300 - left, 300 - top, 0, 0);
- }
- }
- }
- public object Content
- {
- get { return (object)GetValue(ContentProperty); }
- set { SetValue(ContentProperty, value); }
- }
- // Using a DependencyProperty as the backing store for Content. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty ContentProperty =
- DependencyProperty.Register("Content", typeof(object), typeof(CustomLabel), new PropertyMetadata(null,OnContentChanged));
- private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- (d as CustomLabel).CalculateContentPosition();
- }
- public Thickness LabelMargin
- {
- get { return (Thickness)GetValue(LabelMarginProperty); }
- set { SetValue(LabelMarginProperty, value); }
- }
- // Using a DependencyProperty as the backing store for LabelMargin. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty LabelMarginProperty =
- DependencyProperty.Register("LabelMargin", typeof(Thickness), typeof(CustomLabel), new PropertyMetadata(new Thickness(1)));
- }
- public CustomLabel()
- {
- this.SizeChanged += CustomLabel_SizeChanged;
- this.LayoutUpdated += CustomLabel_LayoutUpdated;
- }
- private void CustomLabel_LayoutUpdated(object sender, object e)
- {
- CalculateContentPosition();
- }
Add Comment
Please, Sign In to add comment