【发布时间】:2015-12-18 01:55:44
【问题描述】:
我有一个用户控件,我想禁用 UserControl 中控件的可见性。我只希望当用户的光标悬停在用户控件的主要部分(即“橙色”矩形部分)上时它才可见。红圈是控件的一部分,应该只在“悬停”时可见
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
WindowStartupLocation="CenterScreen"
xmlns:local="clr-namespace:WpfApplication1">
<Grid>
<Canvas >
<Canvas.Background>
<VisualBrush TileMode="Tile" Stretch="Uniform" Viewport="20,20,20,20" ViewportUnits="Absolute">
<VisualBrush.Visual>
<Rectangle Width="20" Height="20" Fill="sc#1,0.01,0.01,.01" Stroke="sc#1,0.02,0.02,.02" StrokeThickness="0.1"/>
</VisualBrush.Visual>
</VisualBrush>
</Canvas.Background>
<local:ShapeNode Canvas.Left="117" Canvas.Top="84"/>
<local:ShapeNode Canvas.Left="242" Canvas.Top="183"/>
</Canvas>
</Grid>
</Window>
UserControl - ShapeNode.xaml
<UserControl x:Class="WpfApplication1.ShapeNode"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Ellipse Fill="Red" Opacity=".2" Height="150" Width="150"></Ellipse>
<Border Margin="5" Height="50" Width="100" Background="#FFDE6119" CornerRadius="5"></Border>
<TextBlock VerticalAlignment="Center" Background="Transparent" Text="Donuts" HorizontalAlignment="Center"></TextBlock>
</Grid>
</UserControl>
【问题讨论】:
-
鼠标进入Border时是否显示椭圆?
-
带触发器。这是鼠标悬停触发答案:stackoverflow.com/a/2388563/424129
-
嘿,又是我的朋友 Ed。嘿!仅当鼠标在矩形范围内时才显示椭圆。
标签: c# wpf xaml user-controls