Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF Mouse
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?
 

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"
xmlnsBig Grin | :-D ="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="97" d:DesignWidth="122">

<Button Content="Add" Height="23" HorizontalAlignment="Left" Name="Add"
VerticalAlignment="Top" Width="75">
<Button.Style>
<Style TargetType="{x:Type Button}">

<Style.Triggers>




</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}">

<Style.Triggers>



</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}">

<Style.Triggers>



</Style.Triggers>
</Style>
</Button.Style>
</Button>

Posted 13-Feb-12 8:25am
DSM77330
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
DSM77 at 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.
 


<Button Content="Add" Height="23" HorizontalAlignment="Left" Name="Add"
VerticalAlignment="Top" Width="75">
<Button.Style>
<Style TargetType="{x:Type Button}">

<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">


</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}">

<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">

</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}">

<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">

</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>

Christian Graus at 13-Feb-12 16:07pm
   
I see the mouse over trigger. Where does it set the visibility though ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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???
  Permalink  
v2
Comments
DSM77 at 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 at 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 at 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)

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 14 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100