如何在WPF中将鼠标悬停在按钮控件上

问题描述:

很抱歉,但我是WPF的新手.我正在使用VS2010.当鼠标移到按钮上但可见性属性不起作用时,我试图将按钮的visible属性设置为Visible.
下面的示例包含三个按钮:
1.一种将文本设置为红色的
2.该按钮应该可见但不可见
3.该按钮应该不可见,但开始闪烁.
有人可以告诉我为什么最后两个按钮上的visible属性不能正常工作吗?
这是项目的一部分,在该项目上,用户控件上将有两个按钮将被添加到Datagrid,并允许用户添加或删除注释. (类似于在Visual Studio中删除选项卡)有人知道这样的示例吗?

< usercontrol x:class ="HideButtons.UserControl1" xmlns:x =#unknown">
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 ="97" d:DesignWidth ="122">
< grid height ="76" width ="92">
<按钮Content ="Add" Height ="23" Horizo​​ntalAlignment ="Left" Name ="Add"
VerticalAlignment =顶部"宽度="75">
< Button.Style>
< Style TargetType ="{x:Type Button}">
< setter property ="Visibility" value ="Visible">
< Style.Triggers>
< trigger property ="IsMouseOver" value ="True"> < setter property ="Control.Foreground" value =#FFF61C1C">
< setter property ="Control.FontWeight" value ="Bold">
</Style.Triggers>
</Style>
</Button.Style>
</Button>

<按钮Content ="Delete" Height ="23" Horizo​​ntalAlignment ="Left" Margin ="0,23,0,0" Name ="Delete"
VerticalAlignment =顶部"宽度="75">
< Button.Style>
< Style TargetType ="{x:Type Button}">
< setter property ="Visibility" value =隐藏">
< Style.Triggers>
< trigger property ="IsMouseOver" value ="True"> < setter property ="Visibility" value ="Visible">
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<按钮Content ="Test" Height ="23" Horizo​​ntalAlignment ="Left" Margin ="0,46,0,0" Name ="Test"
VerticalAlignment =顶部"宽度="75">
< Button.Style>
< Style TargetType ="{x:Type Button}">
< setter property ="Visibility" value ="Visible">
< Style.Triggers>
< trigger property ="IsMouseOver" value ="True"> < setter property ="Visibility" value =隐藏">
</Style.Triggers>
</Style>
</Button.Style>
</Button>

Apologies but I am new to WPF. I am using VS 2010. I am trying to set the visible property of my button to Visible when the mouse moves over the button but the visibility property does not work.
The example below has three buttons:
1. One which sets the text to Red
2. The button should become visible but does not
3. The button should become not visible but starts to flicker.
Can someone please tell me why the visible property does not work correctly on the last two buttons?
This is part of a project where there will be two buttons on a user control that will be added to a Datagrid and allow users to add or delete a comment. (Similar to the delete of a tab in Visual Studio) Does anyone know of an example like this?

<usercontrol x:class="HideButtons.UserControl1" xmlns:x="#unknown">
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="97" d:DesignWidth="122">
<grid height="76" width="92">
<Button Content="Add" Height="23" HorizontalAlignment="Left" Name="Add"
VerticalAlignment="Top" Width="75">
<Button.Style>
<Style TargetType="{x:Type Button}">
<setter property="Visibility" value="Visible">
<Style.Triggers>
<trigger property="IsMouseOver" value="True"> <setter property="Control.Foreground" value="#FFF61C1C">
<setter property="Control.FontWeight" value="Bold">
</Style.Triggers>
</Style>
</Button.Style>
</Button>

<Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="0,23,0,0" Name="Delete"
VerticalAlignment="Top" Width="75">
<Button.Style>
<Style TargetType="{x:Type Button}">
<setter property="Visibility" value="Hidden">
<Style.Triggers>
<trigger property="IsMouseOver" value="True"> <setter property="Visibility" value="Visible">
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button Content="Test" Height="23" HorizontalAlignment="Left" Margin="0,46,0,0" Name="Test"
VerticalAlignment="Top" Width="75">
<Button.Style>
<Style TargetType="{x:Type Button}">
<setter property="Visibility" value="Visible">
<Style.Triggers>
<trigger property="IsMouseOver" value="True"> <setter property="Visibility" value="Hidden">
</Style.Triggers>
</Style>
</Button.Style>
</Button>

我在这里看不到任何代码来设置可见性.我看不到任何触发事件的触发器.我根本看不到任何代码可以执行您想要的操作.首先,我将使用事件并编写代码来设置可见性,这比触发器更容易调试和遵循.可见性属性的效果很好.
I don''t see any code here to set the visibility. I don''t see any triggers to catch events. I see no code at all to do what you want. I would use events and write code to set the visibility at first, that''s easier to debug and follow than triggers. The Visibility property works just fine.


我会为此使用Opacity,当您希望它不可见时将其设置为0,然后在您希望看到它时将其设置为1.

我确实要质疑为什么您会想要这样一个不直观的界面??
I would use Opacity for this, set it to 0 when you want it invisible and then 1 when you want to see it.

I do have to question why you''d want such an unintuitive interface though???