在网格后面绘图

时间:2012-08-31 09:46:00

标签: c# wpf xaml grid drawing

我正在尝试绘制一个在网格文本后面旋转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>

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

1 个答案:

答案 0 :(得分: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>

item template screenshot

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