在矩形WPF中添加内容/文本
这是我的矩形代码:在矩形WPF中添加内容/文本,wpf,xaml,rectangles,Wpf,Xaml,Rectangles,这是我的矩形代码: <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 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>
我的问题是如何在矩形中添加文本/内容
有人可能会建议使用代码块,但如果你仔细阅读我的代码,你会注意到矩形会改变鼠标盖上的颜色。因此,如果我在矩形上放置文本块,鼠标盖将无法正常工作(因为文本块覆盖了整个矩形)
另一个建议是使用边框。但我不确定这一点,因为我需要找到在边框上应用鼠标悬停效果的代码
下一个建议可能是用一个按钮来代替。我会的,但是我的矩形有一个圆角半径,有点圆,如果我用一个按钮,这将很难实现
那么如何在矩形中添加内容/文本呢?如果您觉得一定要使用矩形,请将其放在网格中,并在其上方添加
TextBlock
元素
通过将TextBlock
设置为False
,所有点击测试(鼠标事件)将被忽略,并进入矩形
<Grid>
<Rectangle (...your attributes here...)>
(...your rectangle code here...)
</Rectangle>
<TextBlock Text="Hello World!" IsHitTestVisible="False" />
</Grid>
(…您的矩形代码在此…)
如果您觉得必须使用矩形,请将其放入网格中,并在其上方添加一个TextBlock
元素
通过将TextBlock
设置为False
,所有点击测试(鼠标事件)将被忽略,并进入矩形
<Grid>
<Rectangle (...your attributes here...)>
(...your rectangle code here...)
</Rectangle>
<TextBlock Text="Hello World!" IsHitTestVisible="False" />
</Grid>
(…您的矩形代码在此…)