Вы можете добавить Negative RotateTransform на TextBlock
s.Вам нужно написать NegativeConverter
, который реализует IValueConverter
и связать RotateTransform
из TextBlock
с Grid
.
TextBlocks that not rotating with the Grid
">
Я написал конвертер:
public class NegativeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> value is double number ? -number : 0.0;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotSupportedException();
}
Используйте это NegativeConverter
в вашем XAML:
<Grid.Resources>
<local:NegativeConverter x:Key="NegativeConverter" />
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="RenderTransformOrigin" Value="0.5 0.5" />
<Setter Property="RenderTransform">
<Setter.Value>
<RotateTransform Angle="{Binding (UIElement.RenderTransform).(RotateTransform.Angle), ElementName=RootGrid, Converter={StaticResource NegativeConverter}}" />
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
Код, показанный ниже, является моим полным файлом XAML.Прочитав этот код, вы можете выяснить, как работает это решение:
<Window x:Class="Walterlv.Demo.Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Walterlv.Demo.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Storyboard x:Key="Storyboard.RotateAll">
<DoubleAnimation Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
Duration="0:0:5" From="0" To="360" RepeatBehavior="Forever" />
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard Storyboard="{StaticResource Storyboard.RotateAll}" />
</EventTrigger>
</Window.Triggers>
<Grid x:Name="RootGrid" RenderTransformOrigin="0.5 0.5"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="8" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RenderTransform>
<RotateTransform />
</Grid.RenderTransform>
<Grid.Resources>
<local:NegativeConverter x:Key="NegativeConverter" />
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="RenderTransformOrigin" Value="0.5 0.5" />
<Setter Property="RenderTransform">
<Setter.Value>
<RotateTransform Angle="{Binding (UIElement.RenderTransform).(RotateTransform.Angle), ElementName=RootGrid, Converter={StaticResource NegativeConverter}}" />
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Rectangle Grid.Row="1" Grid.Column="0" Fill="Gray" Width="32" Height="32" Margin="4" />
<Rectangle Grid.Row="1" Grid.Column="1" Fill="Gray" Width="32" Height="32" Margin="4" />
<Rectangle Grid.Row="1" Grid.Column="2" Fill="Gray" Width="32" Height="32" Margin="4" />
<TextBlock Grid.Row="2" Grid.Column="0" RenderTransformOrigin="0.5 0.5" Text="01:30" />
<TextBlock Grid.Row="2" Grid.Column="2" RenderTransformOrigin="0.5 0.5" Text="01:30" />
</Grid>
</Window>