0

我正在尝试绘制一个在网格文本后面旋转 45° 的矩形,并且网格位于 TreeViewItem 内。到目前为止,没有任何事情可以让它正确......我也一直在尝试使用网格的背景,但那里没有结果。这是到目前为止我得到的代码:

<Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeViewItem">
                <Grid Margin="0,5,0,0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Rectangle Name="rect" Stretch="Uniform" Fill="{StaticResource ItemAreaBrush}" 
                                   Stroke="{StaticResource ItemBorderBrush}" StrokeThickness="1">
                        <Rectangle.RenderTransform>
                            <RotateTransform CenterX=".5" CenterY=".5" Angle="45" />
                        </Rectangle.RenderTransform>
                    </Rectangle>
                    <ContentPresenter ContentSource="Header" Grid.Row="0" />
                    <ItemsPresenter Grid.Row="1" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <!--<Setter TargetName="Bd" Property="Panel.Background" Value="{StaticResource SelectedItemAreaBrush}" />-->
                        <Setter TargetName="rect" Property="Rectangle.Stroke" Value="{StaticResource SelectedItemBorderBrush}" />
                        <Setter TargetName="rect" Property="Rectangle.StrokeThickness" Value="2" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

任何人都可以帮助我解决这个问题吗?

4

1 回答 1

1

你是不是这个意思:

<ControlTemplate TargetType="TreeViewItem">
    <Grid Margin="0,5,0,0" Background="Orange">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Rectangle Name="rect"
                    Stretch="Uniform" Fill="Cyan" Stroke="Red" StrokeThickness="1"
                    Grid.Row="0">
            <Rectangle.LayoutTransform>
                <RotateTransform CenterX=".5" CenterY=".5" Angle="45" />
            </Rectangle.LayoutTransform>
        </Rectangle>

        <ContentPresenter ContentSource="Header" Grid.Row="0" />

        <ItemsPresenter Grid.Row="1" />
    </Grid>

    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="true">
            <Setter TargetName="rect" Property="Rectangle.Stroke" Value="{StaticResource SelectedItemBorderBrush}" />
            <Setter TargetName="rect" Property="Rectangle.StrokeThickness" Value="2" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

项目模板截图

请注意,我将 RenderTransform 更改为 LayoutTransform - 前者导致矩形“水平”布局,并且仅在放置其他所有内容后才转身,因此底部“溢出”。LayoutTransform 由 CPU/GPU 处理有点慢,但它实际上处理了新大小的转换后。另外,请注意,现在使用 LayoutTransform 矩形容易居中/边距/等

于 2012-08-31T10:03:29.380 回答