WPF Focus on "run" element - wpf

I would like to change the background of the Run that has the focus in a FlowDocument in a RichTextBox.
I would like to provide to my users a visual cue as to which Run element they are currently editing and I think a light background would be the best way for my application.
I see that the Run has Focusable (which I set to true), IsFocused, FocusVisualStyle, GotFocus, etc. but none of those properties or events seam to work.
Thank you for any help you can give.

You can use a data trigger to get the effect you want. Here's an example:
<Style TargetType="{x:Type RichTextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="BorderBrush" Value="LightGrey" />
</Trigger>
</Style.Triggers>
</Style>

Related

How can I use IsPressed property in ControlTemplate of Thumb?

I wanna to rewrite the ControlTemplate of the Thumb.
Here is the code:
<ControlTemplate x:Key="123" TargetType="Thumb">
<Border Name="B"></Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="B" Value="#575757"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="B" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
However, VS 2019 RC1 reports an error:
It is so strange that the IsMouseOver is all right but the IsPressed reports the error.
I found the official template of Microsoft as below:https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/thumb-styles-and-templates
But the official template now is using the VisualState but not trigger.
I wanna that how can I use the IsPressed property by using the Triggers.
Maybe you will ask me why do not use the VisualState? I wrote several custom controls in the project, I don't want something using the VisualState and something using the Trigger, that's so muddledness. I just want all are Triggers.
Thank you.
Thumb does not have IsPressed property according to Docs
You can use other properties such as IsDragging
Gets whether the Thumb control has logical focus and mouse capture and the left mouse button is pressed.

xctk:IntegerUpDown custom style to hide ButtonSpinner

On the xctk:IntegerUpDown, I would like the textbox border and the ButtonSpinner to only be visible when focused or mouseover.
It is easy enough to turn the border on/off using a <Style.Triggers> section.
It is also possible to control the ShowButtonSpinner property.
However, the content of the TextBox jumps to the right if I set ShowButtonSpinner=False.
I would like to simply hide the ButtonSpinner without TextBox contents jumping around.
Like this:
How can I get access to the appropriate property?
your question helped me to find ShowButtonSpinner property which I needed to hide up and down buttons
i can suggest a workaround with setting a fixed Padding for content when buttons are hidden. Value 0,0,17,0 seems ok to me (Win7, wpf toolkit version v2.6.0.0)
<xctk:IntegerUpDown.Style>
<Style TargetType="xctk:IntegerUpDown">
<Setter Property="Padding" Value="0,0,17,0"/>
<Setter Property="ShowButtonSpinner" Value="False"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Padding" Value="0"/>
<Setter Property="ShowButtonSpinner" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</xctk:IntegerUpDown.Style>
another simple thing is to align text to left side via property
<xctk:IntegerUpDown TextAlignment="Left"/>

How would I increment a color value on MouseOver trigger in XAML template?

Hi all,
I am working on creating a button template to use in my WPF application. The format is basically color tiles (Think Windows Phone style for the most part.).
If I was wanting to change the color of the button in the event of a MouseOver, I would just make some code like this (let's say the original button color is Gray):
<Window.Resources>
<Style x:Key="ColorTileButton" TargetType="Button>
<!-- Insert various property editing here. -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False>
<Setter Property="Background" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</Style>
</Window.Resources>
But obviously this wouldn't work if the original button color was, say, Firebrick because I would end up with a bunch of standard Gray buttons. So I was wondering if there would be a way to rewrite this button template so that it would just increment the values of R, G, and B by about 10 to give the button a lighter color, then decrement is when the mouse left. Help me out?
Thanks guys.
I believe what you are describing is ColorAnimation an example, or you can look at this Forum Post. An interesting note from the Forum post.
A trigger makes a change to one or more properties when its condition is satisfied, and then resets the property values when its condition is no longer satisfied.
So what he is suggesting is:
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray" />
</Trigger>
</ControlTemplate.Triggers>
it would reset to FireBrick as you gave in your example

WPF ComboBox ControlTemplate Background problem

This is an example of a ComboBox's ControlTemplate.
CLICK HERE
I've tried to set the Background / add a trigger to change the background when the ComboBox is focused (with a tab key for example),
both without success.
I don't even understand why it isn't included by default !
(compared to the original generic template)
Do you mean change the background of the ComboBoxItem when it is focused? Its not normal to change the background of an entire ComboBox. Keep in mind that the template is different for editable ComboBoxes.
From looking at the template you referenced, the Background property is used for the ComboBox dropdown. So you're trigger needs to target that outter most Grid. Did you try adding triggers like these?
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter TargetName="[outtermostgrid]" Property="Background" Value="Red" />
</Trigger>
<Trigger Property="IsDropdownOpen" Value="True">
<Setter TargetName="[outtermostgrid]" Property="Background" Value="Red" />
</Trigger>

Make active control background colour change

I have a requirement to change the background colour of the active control (to make it easier to identify where the cursor is).
I've tried using a style with a trigger on the IsFocused property but I'm not having any luck at all; it doesn't seem to fire.
A XAML solution is most preferred.
I kept playing and this seems to work well :)
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="TextBox.Background" Value="Gray" />
</Trigger>
</Style.Triggers>
</Style>

Resources