网格上的RenderTransform(旋转网格但不包含文本内部)

时间:2018-05-28 13:08:31

标签: wpf xaml

我有一个Grid,里面我画了一些形状,我还有两个Textblocks 我正在尝试旋转网格(角度= 90°),但问题是我的TextBlocks内的文字正在与旋转相反

我已尝试将RenderTransformScaleXScaleY一起使用,但它对所有元素都有影响

MyQuestionIs:如何在不影响内部文本的情况下旋转网格(只需旋转形状)

这是我拥有的网格:enter image description here

这就是我想得到的结果:enter image description here

这是我在尝试RenderTransform后得到的:enter image description here

1 个答案:

答案 0 :(得分:1)

您可以在TextBlock上添加Negative RotateTransform。您需要做的是写一个NegativeConverter来实现IValueConverter并将RotateTransform TextBlock绑定到Grid

The <code>TextBlock</code>s that not rotating with the <code>Grid</code>

我写过转换器:

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();
}

在XAML中使用此NegativeConverter

<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>