【问题标题】:Adding content/text in rectangle WPF在矩形 WPF 中添加内容/文本
【发布时间】:2017-12-06 15:36:59
【问题描述】:

这是我的矩形代码:

 <Rectangle x:Name="rect1" Grid.Column="1"  Fill="#FF5C626C" HorizontalAlignment="Left" Height="50" Margin="36,171,0,0" StrokeThickness="2" VerticalAlignment="Top" Width="358" RadiusX="30" RadiusY="50">
        <Rectangle.Triggers>
        </Rectangle.Triggers>
        <Rectangle.Style>

            <Style TargetType="Rectangle">
                <Style.Triggers>

                    <EventTrigger RoutedEvent="Rectangle.MouseEnter">
                        <BeginStoryboard>
                            <Storyboard>
                                <ParallelTimeline  >
                                    <ColorAnimation Storyboard.TargetProperty="Fill.Color" To="#FF767C84" />
                                </ParallelTimeline>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="Rectangle.MouseLeave">
                        <BeginStoryboard>
                            <Storyboard>
                                <ParallelTimeline  >
                                    <ColorAnimation Storyboard.TargetProperty="Fill.Color" To="#FF5C626C" />
                                </ParallelTimeline>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>

我的问题是如何在矩形中添加文本/内容?

有人可能会建议使用代码块,但如果你仔细阅读我的代码,你会注意到矩形在鼠标悬停时会改变它的颜色。所以如果我在矩形上放置一个文本块,鼠标悬停就不能正常工作(因为文本块覆盖了整个矩形)。

另一个建议是使用边框。但我不确定这一点,因为我需要找到代码以在边框上应用鼠标悬停效果。

下一个建议可能是使用按钮。我会的,但我的矩形有圆角半径并且有点圆形,如果我使用按钮,将很难实现。

那么如何在矩形内添加内容/文本?

【问题讨论】:

    标签: wpf xaml rectangles


    【解决方案1】:

    如果您觉得必须使用矩形,请将其放入网格中并在其上方添加 TextBlock 元素。

    通过将TextBlockIsHitTestVisible property 设置为False,所有的命中测试(鼠标事件)都将被忽略并落入您的矩形。

    <Grid>
        <Rectangle (...your attributes here...)>
            (...your rectangle code here...)
        </Rectangle>
        <TextBlock Text="Hello World!" IsHitTestVisible="False" />
    </Grid>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-16
      • 2016-12-14
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多