Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the thing, I created a usercontrol for my application to show something. To make it more beautiful, I want to implement some kind of effect that the border of this usercontrol will change color when the mouse hover over it (as if it gets focus). Here is the structure of my usercontrol:
HTML
<UserControl ......>
    <Border Name="questionBorder" BorderThickness="3" BorderBrush="Transparent">
        <Grid>......</Grid>
    </Border>
</UserControl>

I remember that there is an event named MouseHover, but sadly I couldn't find it.
I apprieciate your help ;)

Supplementary: When I tried "UserControl_MouseEnter"
C#
private void UserControl_MouseEnter(object sender, MouseEventArgs e)
{
    questionBorder.BorderBrush = new SolidColorBrush(Color.FromRgb(9, 163, 220));
}

and "UserControl_MouseLeave"
C#
private void UserControl_MouseLeave(object sender, MouseEventArgs e)
{
    questionBorder.BorderBrush = Brushes.Transparent;
}

they kind of helped me. However, when my mouse was hovering over the blank area (still inside the area of usercontrol), the border became transparent.
Posted
Updated 30-Aug-15 21:42pm
v2

That is quiet right because, when the Mouse is over a Sub-Control of your UserControl, it isn't anymore over the UserControl itself. It's a part of the philosophie ... The Mouse could allways only be over one Control.

What you can do now is :
You ask in the MouseLeave-Method if the UserControl-Rectangle contais the actual MousePosition. If not change the Border.
Therefore it is necessary that no inner Control directly bounds to the outside of the UserControl.

(Code removed)
 
Share this answer
 
v2
Comments
Wu Jiecheng 31-Aug-15 6:00am    
Thanks for your brilliant idea, but it seems that my usercontrol (derived from "UserControl") doesn't have the definitions of "RectangleToScreen" and "ClientRectangle":(
Is there any way I can fix this?
Ralf Meier 31-Aug-15 9:14am    
I'm sorry ... but before I wrote it I tested it(but in the code of the UserControl itself - but that not make any difference).
Which .Net-Version are you using ? (I tried it with .Net-Framework 4.0).
Wu Jiecheng 31-Aug-15 10:14am    
Thanks for your patient explanation.
I'm using a .Net Framework 4.5.
Ralf Meier 31-Aug-15 10:40am    
I think that doesn't make the difference. It must be anywhere else. The posted methods are also content of this framework.
What Assemblies are part of your solution ?
Ralf Meier 31-Aug-15 10:48am    
I'm sorry ... I don't know anymore how I created it with that code - I can't reproduce it - bet for apologize ... (I'm a Forms-Developer).
But I think, you are right. So I looked for a Link for you. Perhaps it helps you further :
http://stackoverflow.com/questions/6931333/wpf-converting-between-screen-coordinates-and-wpf-coordinates

Another way could be :
You catch the Entered-Event of each internal Control too and do the same as with the Entered-Event of the UserControl itself. But for those elements don't use the Leaved-Event ...
The class System.Windows.UIElement (from which UserControl indirectly derives) has a dependency property called IsMouseOver. You could use a Trigger in your .xaml-file for setting the border color depending on the value (True/False) of this property.

Here ist a good explanation how to achieve this behaviour:
http://www.wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/

Edit:

I've made an example which works by only using Triggers:

<Window x:Class="BorderColor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
      
        <UserControl BorderThickness="3">
            <UserControl.Style>
                <Style TargetType="UserControl">
                    <Setter Property="BorderBrush" Value="Blue"></Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </UserControl.Style>

            <Grid>
                <TextBlock Text="Hello!" FontSize="28" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextAlignment="Justify">
                </TextBlock>
            </Grid>

        </UserControl>        
        
</Window>


The important thing here is, that the border of the UserControl itself is used instead of an extra border. Additionally, the whole UserControl has to be filled with content (therefore the alignments of the TextBox are set to Stretch). So, you can put inside the Grid whatever you want, just make sure all the available space of the UserControl ist used/filled.

I hope this is what you wanted to achieve.

Edit 2:

To avoid those "empty spaces", you could e.g. insert an empty label in your grid with the appropriate row and column spans. A simple example (without row or column definitions):

<Grid>
    <Label Width="Auto" Height="Auto"></Label>
    <TextBlock Text="Hello!" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center">
    </TextBlock>
</Grid>
 
Share this answer
 
v3
Comments
Wu Jiecheng 1-Sep-15 21:43pm    
I've tried the trigger way, it's the same effect as I implement the Mouse_Enter & Mouse_Leave events. Namely, when I put the mouse over the Border or its sub-controls, the BorderBrush property changes ( e.g. blue). But when I put the mouse over the blank area ( still inside the Border), the BorderBrush property becomes transparent again. It seems that the Trigger and Mouse events both don't work well for a container ( Border, Grid and so on).
U. G. Leander 2-Sep-15 3:53am    
I just updated my solution and provided an example. Please take a look at it and let me know if this is the functionality you want to achieve.
Wu Jiecheng 2-Sep-15 9:35am    
This Stretch thing perfectly solve my question! I've achieved what I wanted! Thank you so much:)
U. G. Leander 2-Sep-15 9:39am    
You are welcome!

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