Mouse Hower on TextBlock - wpf

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

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 - 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.

WPF Grid IsMouseOver Property

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

WPF LinkLabel implementation strategy

I am attempting to create a LinkLabel control for WPF. Essentially, I'm going to create the LinkLabel from a TextBlock and handle MouseEnter, MouseLeave, and MouseLeftButtonUp events. In the back end I have a base class that has properties that you would expect to see with a LinkLabel. With most other clickable controls in WPF, there is a default MouseEnter animation where the control becomes Ice Blue. I would like to duplicate this behavior when the mouse cursor enters over the TextBlock. I'm not sure if I'm needing to derive from ButtonBase or something along those lines. I have a I am able to change the cursor to a hand, and handle the event for when the "LinkLabel" is clicked. If accessing this seemingly default color changing animations, then I just may have to resort to a simple foreground color swap without the smooth transition. If anyone has created a custom WPF LinkLabel before or has any advice into the matter your input would be much appreciated.
You can create the equivalent of WinForms' LinkLabel right now using a combination of TextBlock and HyperLink:
<TextBlock>Here is a <Hyperlink NavigateUri="http://example.com">link</Hyperlink></TextBlock>
You won't get the "ice blue" mouse-over effect, but you will get the hand cursor. I'm sure you can introduce your mouse-over effects using a simple style trigger.
The "NavigateUri" property works in navigation-style applications where the hyperlink is inside a Frame control. In a standard WPF application you'll want to handle the Hyperlink's Click event instead and use Process.Start to kick off a web browser with the correct URL.
I just created a style for a button and apply a style to a button whenever you want LinkLabel look. Click event of button is used to perform a function when the text is clicked.
<Style x:Key="LinkLabelButtonStyle" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="CornflowerBlue"></Setter>
</Trigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="Transparent">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextDecorations="Underline" Text="{TemplateBinding Content}"></TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="DarkBlue"></Setter>
<Setter Property="MinWidth" Value="90"></Setter>
<Setter Property="HorizontalAlignment" Value="Left"></Setter>
<Setter Property="Padding" Value="5"></Setter>
<Setter Property="Margin" Value="5"></Setter>
<Setter Property="Cursor" Value="Hand"></Setter>
</Style>
You can place above style in the Window.Resources so you can use it in entire window.
Then apply the style to a button whenever you want LinkLabel look.
<Button Name="LinkLabelLookALikeButton" Content="Text goes here" Style="{StaticResource LinkLabelButtonStyle}" Click="Event_Goes_Here">
</Button>
Hope this helps!

Resources