WPF Grid IsMouseOver Property - wpf

The WPF Grid has an "IsMouseOver" property that you can use in the Grid's Style's Triggers.
My problem is that the "IsMouseOver" property only changes if the mouse is over some control (ie a Button, or ComboBox) within the Grid itself.
For example:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Column="1">A Button</Button>
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Opacity" Value="0.5"></Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="1"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
The above Grid and it's contents will be displayed in half opacity so that you can see the controls.
You will notice that the opacity will not be set to full if you hover over the first column (which doesn't contain anything).
However the opacity will be set to full if you hover over the button in the second column.
In my application, the Grid that I'm setting the triggers for is being displayed on top of an image control. I do not want the Grid to be displayed until the mouse is hovering over the image... In other words, since the Grid is on top of the image, I don't want the grid to be displayed until the mouse is hovering over the Grid (anywhere in the grid) because the Grid is on top of the image.
Does anyone know how to accomplish this?
Thanks!
-Frinny

Your problem is that the Grid itself is not hit-testable because it has no background. Try this instead:
<Grid Background="Transparent">

set the grids background to transparent, then it should work
for details why this is so please look here

Related

Need to change border color in grid using control template and data trigger in WPF

i am new in WPF. My project have one Grid Control that grid control border is Red color and I have a one button. when i click the button grid border color will be changed to green.
How can i changed to border color using control template with DataTemplate trigger. My Goal is border color will be change using template(don't change programmatically)
Screenshot will be attached.enter image description here
You can create a property called for example IsColorChanged and bind to your DataTrigger and when button is clicked set this boolean typed property to true on the code side then DataTrigger will set to background property of your border to the green.
<Grid>
<Border BorderThickness="2" CornerRadius="4">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsColorChanged}" Value="True">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</Grid>

IsMouseOver trigger for DockPanel not firing unless children are hovered over

This question has been asked a couple of times before, but at this time, none of the answers I've found turned out to be working.
So I have a custom DockPanel with 2 child elements inside, 1 TextBox and 1 Image.
I intended to have the DockPanel change color on hover, so I put a Trigger inside of it. But currently, it only changes color if I hover over the children, whereas a hover over whitespace between elements doesn't do anything.
I've read similar posts which mentioned using isHitTestVisible, but it doesn't do anything.
Short version of my code is the following:
<DockPanel>
<!-- Change color on mouse hover -->
<DockPanel.Style>
<Style>
<Style.Triggers>
<Trigger Property="DockPanel.IsMouseOver" Value="True">
<Setter Property="DockPanel.Background" Value="#FFe6ffe6"/>
</Trigger>
</Style.Triggers>
</Style>
</DockPanel.Style>
<StackPanel Orientation="Vertical" DockPanel.Dock="Center">
<!-- TextBox and Image code here -->
</StackPanel>
</DockPanel>
Add this to your dock panel style.
<Trigger Property="DockPanel.IsMouseOver" Value="False">
<Setter Property="DockPanel.Background" Value="Transparent"/>
</Trigger>

WPF visibility change of window by mouse pointer change

I am develovp my application in WPF, it is screen recording app. So when the user click on record menu, a window will open and recording the screen. I want to change the recording screen based on mouse pointer change. When the user start the record the window will close, and if the user move the pointer into a particular point in the window, need to show the record window options.
You could position a transparent control in the area where you want the mouse pointer to be in when the record options need to show.
Then use a data trigger to alter the visibility of the record options.
See below for an example which demonstrates this. Note that there is a 2-row grid into which the hover area and the recording area would be placed, but you can adapt this.
<Grid Name="GridMouseHover" Background="Transparent" Grid.Row="0" />
<Grid Background="Red" Grid.Row="1">
<Grid.Resources>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=GridMouseHover}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=GridMouseHover}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Resources>
</Grid>

WPF - How to Change Mouse Cursor Color

I am trying to change the color of the mouse cursor when it's hovering over a textbox, so that it's easier to see on a dark background.
I know how to change four things:
Textbox background color (.Background)
Textbox foreground color (.Foreground)
Textbox caret color (.CaretBrush)
Mouse cursor image (Mouse.OverrideCursor or this.Cursor)
I just can't change the mouse cursor color.
I came across a way to completely change the mouse cursor to a custom made cursor in another question someone posted: "Custom cursor in WPF?". But it seems overkill for just wanting to change the color, so that I can actually see where the mouse is.
The mouse cursor color actually changes to white automatically if the textbox has a black background. But does not change automatically if it has a dark background that isn't quite black.
It's this simple. Try changing the CaretBrush color. See sample code below.
<TextBox Text="This is some random text" CaretBrush="Blue" />
EDIT :
You can't change the color of the mouse color without defining a custom cursor, but you can change it's type. See the example below.
<Grid>
<TextBox Width="70" Height="20" CaretBrush="IndianRed" Text="TEST">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Pen" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Cursor" Value="Arrow" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
If you want to change the cursor type see this post Custom cursor in WPF?
You can change colour of the cursor with using CaretBrush property at WPF.
For example:
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="CaretBrush" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
You can add your own trigger conditions if you want.

Mouse Hower on TextBlock

The idea is when mouse howler above TextBlock, new Image is appear and it possible to click on it. When mouse leave the TextBlock - Image should disappear.
Meanwhile I came to this, but still unable to continue:
<Style x:Key="HoverHighlightTextStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="16"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="Margin" Value="3,0,3,0"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
</Trigger>
</Style.Triggers>
</Style>
Expected result
I suggest you to follow the below steps.
Create a stackpanel (orientation is set to horizontal) or any other appropriate control and add text box and image controls inside it.
Hide the borders of text box and the image controls so that only the stackpanel border is visible.
When the mouseover event fired, set the image control to be appeared.
When the mouseleave event fired, set the image control to be disappeared.
I haven't tested this. But this might help you to get an idea.
I find solution based on sa_ddam213 answer:
<TextBlock x:Name="txtblkSelectedItem" Text="My Textblock">
<TextBlock.ToolTip>
<ToolTip PlacementTarget="{Binding ElementName=txtblkSelectedItem}" Placement="Right" HorizontalOffset="-20">
<Image Source="http://stackoverflow.com/users/flair/1849109.png" Width="10" Height="10"/>
</ToolTip>
</TextBlock.ToolTip>
The idea, is positioning tooltip inside textblock can be achived by using ToolTip
Placement="Right" HorizontalOffset="-20"
properties

Resources