在 StackPanel WPF 中使用 MouseOver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28406134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using MouseOver in StackPanel WPF
提问by Bharathi
I have just started learning WPFand trying to hide a StackPanelduring MouseOver. Below is the code that I use. I can only see the Panelflickering when mouse is placed on it but, it doesn't hide completely. Am I missing something here? Thanks in advance.
我刚开始学习WPF,并试图隐藏StackPanel期间MouseOver。下面是我使用的代码。Panel当鼠标放在上面时,我只能看到闪烁,但它并没有完全隐藏。我在这里错过了什么吗?提前致谢。
<Style x:Key="myStyle" TargetType="{x:Type StackPanel}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Visibility" Value="Hidden" />
</Trigger>
<Trigger Property="IsMouseOver" Value="false">
<Setter Property="Visibility" Value="Visible" />
</Trigger>
</Style.Triggers>
</Style>
Stackpanel:
堆栈面板:
<StackPanel Style="{StaticResource myStyle}">
// Child controls
</StackPanel>
回答by Clemens
When the StackPanel is hidden, the IsMouseOverproperty toggles to false, which makes the StackPanel visible again.
当 StackPanel 隐藏时,该IsMouseOver属性切换为false,这使 StackPanel 再次可见。
You might set the Opacityproperty instead of Visibility:
您可以设置Opacity属性而不是Visibility:
<Style x:Key="myStyle" TargetType="{x:Type StackPanel}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Opacity" Value="1" />
</Trigger>
</Style.Triggers>
</Style>
Or, as pointed out in the other answer, declare just one Trigger for IsMouseOver == true:
或者,正如在另一个答案中指出的那样,仅声明一个 Trigger for IsMouseOver == true:
<Style x:Key="myStyle" TargetType="{x:Type StackPanel}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
回答by Steven Rands
Clemens has already answered your question, but just FYI when you are triggering on a Boolean value, you don't need a trigger for bothstates. Just set a single trigger for the trueor falsestate, then when the state no longer applies the properties that were changed by the setters in the trigger will revert back to their previous values. This will cut down on the amount of XAML you need to write.
Clemens 已经回答了您的问题,但仅供参考,当您在布尔值上触发时,您不需要两个状态的触发器。只需为true或false状态设置一个触发器,然后当状态不再应用触发器中的设置器更改的属性时,它们将恢复为以前的值。这将减少您需要编写的 XAML 数量。

