gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:39:27 25 4
gpt4 key购买 nike

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

enter image description here

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>

用户控件 - 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>

最佳答案

我宁愿使用可以在 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>

关于c# - wpf - 如何控制用户控件鼠标悬停时的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34347391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com