Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
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>

Posted

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.
 
Share this answer
 
Comments
DSM77 13-Feb-12 15:49pm    
Thanks for your response and apologies, when I copied not all the code came with it. As you can see the text changes colour without an issue so should the visibility of the button be a problem. I intend to add the style to a ResourceDictionary but for test purposes it is all in the Xaml.

<usercontrol x:class="HideButtons.UserControl1"
="" 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">
</Trigger>
</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">
</Trigger>
</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">
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>

Christian Graus 13-Feb-12 16:07pm    
I see the mouse over trigger. Where does it set the visibility though ?
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???
 
Share this answer
 
v2
Comments
DSM77 14-Feb-12 5:15am    
I am looking to create a user object that I can add to a variety of places where I can add or delete a comment. The buttons should appear invisible until the user rolls the mouse over a set field. Once highlighted the user can then add a further comment or delete an existing comment. Something similar to Visual Studio and Internet Explorer whereby the tab you are on has a ‘X’ to close but the other tabs do not. When then have focus the ‘X’ appears but when you hover over the tab the cross will appear. It was also a requirement from the users.
SteveAdey 14-Feb-12 5:28am    
Ok, I understand now, but you don't want the trigger on the buttons, you'll need it on the element that is going to contain the buttons. Again I would use opacity because then you can add a storyboard to make a nice fade in/out animation.
DSM77 14-Feb-12 7:38am    
Thank you for your response. It looks like this will be the answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900