在矩形 WPF 中添加内容/文本 [英] Adding content/text in rectangle WPF

查看:54
本文介绍了在矩形 WPF 中添加内容/文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的矩形代码:

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

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

My question is how do i add a text/content in the rectangle ?

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

One might suggest using a code block, but if u go through my code, youu'll notice that the rectangle changes it's color on mouseover.So if i put a textblock over the rectangle,the mouseover doesn't work properly(as the textblock covers the entire rectangle).

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

Another suggestion would be to use a border.But i am not sure about this as i need to find the code to apply mouse over effect on a border.

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

The next suggestion might be to use a button instead.I would've but my rectangle has corner radius and is a bit round-shaped which, if i use a button, would be hard to achieve.

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

推荐答案

如果您觉得一定要使用矩形,请将其放入网格中并在其上方添加 TextBlock 元素.

If you feel you definitely have to use a rectangle, put it in a grid and add a TextBlock element above it.

通过设置 TextBlockIsHitTestVisible 属性False 所有命中测试(鼠标事件)将被忽略它并落入您的矩形.

By setting the TextBlock's IsHitTestVisible property to False all hit-testing (mouse events) will be ignored on it and fall through to your rectangle.

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

这篇关于在矩形 WPF 中添加内容/文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆