【问题标题】:How to draw evenly spaced diagonal text with wpf xaml如何使用 wpf xaml 绘制均匀间隔的对角线文本
【发布时间】:2019-03-12 10:55:20
【问题描述】:

我正在做一个自定义 WPF UserControl,我需要绘制一个可变大小的文本,该文本旋转 45 度并水平均匀分布,就像下一个图像(作为文本的红色条):

使用以下代码:

    <UserControl.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="CheckTemplate">


                <!-- description -->
                <TextBlock 
                    VerticalAlignment="Bottom" Margin="-10,0,0,0" Text="{Binding Check.Name}" Background="Transparent" x:Name="AAA">
                    <TextBlock.LayoutTransform>
                        <RotateTransform Angle="-45" />
                    </TextBlock.LayoutTransform>
                </TextBlock>



            <DataTemplate.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Margin" Value="0" TargetName="AAA" />
                </Trigger>
            </DataTemplate.Triggers>

        </DataTemplate>

        <ItemsPanelTemplate x:Key="ChecksItemsPanel">
            <StackPanel Orientation="Horizontal" 
                        HorizontalAlignment="Left" 
                        VerticalAlignment="Bottom" 
                        />
        </ItemsPanelTemplate>
    </ResourceDictionary>
</UserControl.Resources>

    <StackPanel x:Name="RootPanel" Margin="5">

    <ItemsControl
        x:Name="WorkflowChecksItemsControl"
        ItemTemplate="{DynamicResource CheckTemplate}"
        ItemsPanel="{DynamicResource ChecksItemsPanel}" 
        ItemsSource="{Binding WorkflowChecks}" />



</StackPanel>

我只做了这样的事情:

如何使用 XAML 做到这一点? 在这个项目中,我也在使用 Telerik UI for WPF,如果它更简单,我可以使用他们的框架。

【问题讨论】:

    标签: c# wpf xaml telerik wpf-controls


    【解决方案1】:

    您可以将 ItemsPanel 的 -90° LayoutTransform 与每个 TextBlock 的 45° RenderTransform 结合起来。对于水平距离,只需设置 TextBlocks 的高度即可。

    <ItemsControl ItemsSource="{Binding WorkflowChecks}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel>
                    <StackPanel.LayoutTransform>
                        <RotateTransform Angle="-90"/>
                    </StackPanel.LayoutTransform>
                </StackPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Check.Name}" RenderTransformOrigin="0,1">
                    <TextBlock.RenderTransform>
                        <RotateTransform Angle="45"/>
                    </TextBlock.RenderTransform>
                </TextBlock>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    结果:

    【讨论】:

      猜你喜欢
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 2014-11-20
      • 2021-02-07
      • 1970-01-01
      相关资源
      最近更新 更多