I have a window which overrides a RadioButton's ControlTemplate to show a custom control inside of it. Inside the custom control, I have a button's visibility tied to IsMouseOver, which works correctly in showing the button only when the mouse is hovering over the control. However, when I click on the RadioButton, the Button disappears. After some debugging and reading, it seems that the RadioButton is capturing the Mouse on click, and this makes IsMouseOver for the UserControl false.
I tried binding the Button's visibility to FindAncestor {x:Type RadioButton} and it works, but it seems a bit fragile to me to have the UserControl depend on who is containing it. The code for the window and the User Control is below. Any suggestions?
<Window.Resources>
<Style
TargetType="{x:Type RadioButton}">
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type RadioButton}">
<WPFTest:TestUC></WPFTest:TestUC>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Border
BorderBrush="Black"
BorderThickness="2">
<StackPanel>
<RadioButton
x:Name="OptionButton"
Height="100"
>
</RadioButton>
<TextBlock
Text="{Binding ElementName=OptionButton, Path=IsMouseOver}" />
</StackPanel>
</Border>
<UserControl.Resources>
<BooleanToVisibilityConverter
x:Key="BooleanToVisibilityConverter" />
</UserControl.Resources>
<StackPanel>
<TextBlock
Text="SomeText" />
<TextBlock
Text="{Binding ElementName=_this, Path=IsMouseOver}" />
<Button
x:Name="_cancelTextBlock"
Content="Cancel"
Visibility="{Binding ElementName=_this, Path=IsMouseOver, Converter={StaticResource BooleanToVisibilityConverter}}">
</Button>
</StackPanel>