wpf - 如何控制用户控件鼠标悬停的可见性?

Jok*_*ini 2 c# wpf xaml user-controls

我有一个用户控件,我想禁用 UserControl 中控件的可见性。我只希望当用户的光标悬停在用户控件的主要部分(即“橙色”矩形部分)上时它才可见。红色圆圈是控件的一部分,应该只在“悬停”时可见

在此处输入图片说明

主窗口.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>
Run Code Online (Sandbox Code Playgroud)

用户控件 - 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>
Run Code Online (Sandbox Code Playgroud)

tgp*_*dyk 5

我宁愿使用可以在 UserControl 中进行模板化的控件。我最喜欢的是一个按钮——这是因为如果有任何用处的话,这是因为单击事件。但是你可以使用其他的。

<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">
  <Button>
    <Button.Template>
        <ControlTemplate>
            <Grid x:Name="MyGrid" >
                <Ellipse x:Name="MyEllipse" Visibility="Hidden" Fill="Red" Opacity=".2" Height="150" Width="150"/>
                <Border Margin="5" Height="50" Width="100" Background="#FFDE6119" CornerRadius="5"></Border>
                <TextBlock VerticalAlignment="Center" Background="Transparent" Text="Donuts" HorizontalAlignment="Center"></TextBlock>
            </Grid>
        <ControlTemplate.Triggers>
            <Trigger SourceName="MyGrid" Property="IsMouseOver" Value="True">
                <Setter TargetName="MyEllipse" Property="Visibility" Value="Visible"></Setter>
            </Trigger>
        </ControlTemplate.Triggers>
        </ControlTemplate>
     </Button.Template>
  </Button>
</UserControl>
Run Code Online (Sandbox Code Playgroud)