Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF - Binding IsMouseOver to Visibility
    text
    copied!<p>I have a window which overrides a <code>RadioButton</code>'s <code>ControlTemplate</code> to show a custom control inside of it. Inside the custom control, I have a button's visibility tied to <code>IsMouseOver</code>, which works correctly in showing the button only when the mouse is hovering over the control. However, when I click on the <code>RadioButton</code>, the <code>Button</code> disappears. After some debugging and reading, it seems that the <code>RadioButton</code> is capturing the mouse on click, and this makes <code>IsMouseOver</code> for the <code>UserControl</code> false.</p> <p>I tried binding the <code>Button</code>'s visibility to <code>FindAncestor {x:Type RadioButton}</code> and it works, but it seems a bit fragile to me to have the <code>UserControl</code> depend on who is containing it. The code for the window and the user control is below. Any suggestions?</p> <pre><code>&lt;Window x:Name="window" x:Class="WPFTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WPFTest="clr-namespace:WPFTest" Title="Window1" Height="300" Width="300"&gt; &lt;Window.Resources&gt; &lt;Style TargetType="{x:Type RadioButton}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type RadioButton}"&gt; &lt;WPFTest:TestUC /&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Border BorderBrush="Black" BorderThickness="2"&gt; &lt;StackPanel&gt; &lt;RadioButton x:Name="OptionButton" Height="100" /&gt; &lt;TextBlock Text="{Binding ElementName=OptionButton, Path=IsMouseOver}" /&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/Window&gt; &lt;UserControl x:Name="_this" x:Class="WPFTest.TestUC" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"&gt; &lt;UserControl.Resources&gt; &lt;BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /&gt; &lt;/UserControl.Resources&gt; &lt;StackPanel&gt; &lt;TextBlock Text="SomeText" /&gt; &lt;TextBlock Text="{Binding ElementName=_this, Path=IsMouseOver}" /&gt; &lt;Button x:Name="_cancelTextBlock" Content="Cancel" Visibility="{Binding ElementName=_this, Path=IsMouseOver, Converter={StaticResource BooleanToVisibilityConverter}}" /&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload