Выравнивание содержимого метки элемента XAML на холсте сетки - PullRequest
0 голосов
/ 28 ноября 2018

Я пытаюсь изменить уникальный элемент пользовательского интерфейса XAML (не созданный мной), чтобы содержимое его метки центрировалось по центру элемента (Content = "{Binding Counter}"). До сих пор я пытался использовать Margins, Vertical и дажеГоризонтальное выравнивание без лучшей идеи (добавление и изменение существующих)

Есть идеи?

Код и картинка ниже:

<Grid Margin="10,10,0,0" 
              Visibility="{Binding Path=DataContext.Visibility, RelativeSource={RelativeSource TemplatedParent}}" 
              IsEnabled="{Binding Path=DataContext.Enabled, RelativeSource={RelativeSource TemplatedParent}}">
            <Grid Height="{Binding Path=ActualHeight, ElementName=lbl}" Width="{Binding Path=ActualHeight, ElementName=lbl}"
                                      Margin="-195,-95,0,0" Panel.ZIndex="300" ClipToBounds="False" Visibility="{Binding CounterVisibility}">
                <Canvas>
                    <Ellipse Fill="{DynamicResource AlarmNotificationHomeView}" Stroke="{DynamicResource AlarmNotificationStrokeHomeView}" 
                             StrokeThickness="3" Width="50" Height="50" Canvas.Left="0" Canvas.Top="0" Canvas.Right="50" Canvas.Bottom="50" Name="Ellipse" />
                    <Label Name="lbl" Padding="11,13,10,10" HorizontalAlignment="Center" VerticalAlignment="Center"
                           Content="{Binding Counter}" FontWeight="Bold" FontSize="16" Foreground="{DynamicResource AlarmNotificationForegroundHomeView}"/>
                    <Canvas.Triggers>
                        <EventTrigger RoutedEvent="Canvas.Loaded">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="Ellipse" 
                                     Storyboard.TargetProperty="Width"
                                     From="50" To="65" Duration="0:0:0.8"
                                     AutoReverse="True"
                                     RepeatBehavior="Forever" />

                                    <DoubleAnimation Storyboard.TargetName="Ellipse"
                                     Storyboard.TargetProperty="Height"
                                     From="50" To="65" Duration="0:0:0.8"
                                     AutoReverse="True"
                                     RepeatBehavior="Forever" />

                                    <DoubleAnimation Storyboard.TargetName="Ellipse"
                                     Storyboard.TargetProperty="(Canvas.Left)"
                                     From="0" To="-5" Duration="0:0:0.8"
                                     AutoReverse="True"
                                     RepeatBehavior="Forever" />

                                    <DoubleAnimation Storyboard.TargetName="Ellipse"
                                     Storyboard.TargetProperty="(Canvas.Top)"
                                     From="0" To="-5" Duration="0:0:0.8"
                                     AutoReverse="True"
                                     RepeatBehavior="Forever" />
                                </Storyboard>
                            </BeginStoryboard>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="lbl" 
                                     Storyboard.TargetProperty="FontSize"
                                     From="16" To="20" Duration="0:0:0.8"
                                     AutoReverse="True"
                                     RepeatBehavior="Forever" />
                                </Storyboard>
                            </BeginStoryboard>

                        </EventTrigger>
                    </Canvas.Triggers>
                </Canvas>
            </Grid>

enter image description here

1 Ответ

0 голосов
/ 28 ноября 2018

Вместо анимации Canvas.Left / Canvas.Top и Width / Height: как насчет использования RenderTransform и Grid

        <Grid Margin="10" Visibility="{Binding Path=DataContext.Visibility, RelativeSource={RelativeSource TemplatedParent}}" 
          IsEnabled="{Binding Path=DataContext.Enabled, RelativeSource={RelativeSource TemplatedParent}}">
            <Ellipse Fill="{DynamicResource AlarmNotificationHomeView}" Stroke="{DynamicResource AlarmNotificationStrokeHomeView}" StrokeThickness="3" Width="50" Height="50" Name="Ellipse" RenderTransformOrigin="0.5,0.5">
                <Ellipse.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform CenterX="0.5" CenterY="0.5" />
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Ellipse.RenderTransform>
            </Ellipse>
            <Label Name="lbl" HorizontalAlignment="Center" VerticalAlignment="Center"
                       Content="{Binding Counter}" FontWeight="Bold" FontSize="16" Foreground="White" RenderTransformOrigin="0.5,0.5" Foreground="{DynamicResource AlarmNotificationForegroundHomeView}">
                <Label.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform CenterX="0.5" CenterY="0.5" />
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Label.RenderTransform>
            </Label>
            <Grid.Triggers>
                <EventTrigger RoutedEvent="Grid.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard RepeatBehavior="Forever"  AutoReverse="True">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="Ellipse">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="1">
                                        <EasingDoubleKeyFrame.EasingFunction>
                                            <ExponentialEase EasingMode="EaseInOut"/>
                                        </EasingDoubleKeyFrame.EasingFunction>
                                    </EasingDoubleKeyFrame>
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="1.3">
                                        <EasingDoubleKeyFrame.EasingFunction>
                                            <ExponentialEase EasingMode="EaseInOut"/>
                                        </EasingDoubleKeyFrame.EasingFunction>
                                    </EasingDoubleKeyFrame>
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="Ellipse">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="1">
                                        <EasingDoubleKeyFrame.EasingFunction>
                                            <ExponentialEase EasingMode="EaseInOut"/>
                                        </EasingDoubleKeyFrame.EasingFunction>
                                    </EasingDoubleKeyFrame>
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="1.3">
                                        <EasingDoubleKeyFrame.EasingFunction>
                                            <ExponentialEase EasingMode="EaseInOut"/>
                                        </EasingDoubleKeyFrame.EasingFunction>
                                    </EasingDoubleKeyFrame>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                        <BeginStoryboard>
                            <Storyboard RepeatBehavior="Forever"  AutoReverse="True">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="lbl">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="1">
                                        <EasingDoubleKeyFrame.EasingFunction>
                                            <ExponentialEase EasingMode="EaseInOut"/>
                                        </EasingDoubleKeyFrame.EasingFunction>
                                    </EasingDoubleKeyFrame>
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="1.3">
                                        <EasingDoubleKeyFrame.EasingFunction>
                                            <ExponentialEase EasingMode="EaseInOut"/>
                                        </EasingDoubleKeyFrame.EasingFunction>
                                    </EasingDoubleKeyFrame>
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="lbl">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="1">
                                        <EasingDoubleKeyFrame.EasingFunction>
                                            <ExponentialEase EasingMode="EaseInOut"/>
                                        </EasingDoubleKeyFrame.EasingFunction>
                                    </EasingDoubleKeyFrame>
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="1.3">
                                        <EasingDoubleKeyFrame.EasingFunction>
                                            <ExponentialEase EasingMode="EaseInOut"/>
                                        </EasingDoubleKeyFrame.EasingFunction>
                                    </EasingDoubleKeyFrame>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>

                    </EventTrigger.Actions>
                </EventTrigger>
            </Grid.Triggers>
        </Grid>
...