I want to gray out text in the WPF text block. how do i make it?
Regards
Raju
On C#:
textBox.Foreground = Brushes.Gray;
On XAML:
<TextBox Foreground="Gray" />
To disable it (will change background too):
textBox.IsEnabled = false;
The IsEnabled flag for a textblock does not grey the text. This post details the differences between textblock and label. It also shows the XAML to add a trigger on IsEnabled to grey the text.
You can set the TextBlock.Foreground property to any color (technically, any Brush). If you want it to be grayed out, just set:
<TextBlock Text="Foo" Foreground="Gray" />
If you want it to look "disabled", you can set IsEnabled to false:
<TextBlock Text="Foo" IsEnabled="false" />
TextBlocks do not grayout automaticly when disabled
you can use a style to do this for you
<Style x:Key="DisableEnableTextBlock" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Opacity" Value="1" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value=".5" />
</Trigger>
</Style.Triggers>
</Style>
The trouble with using the TextBox is that there's a box round it. If you use Label (with Content="Foo") then you can toggle the text colour with IsEnabled. Otherwise it behaves like TextBlock for a short heading/label.
For WinUI set the Opacity property to something around 0.5.
Use TextBox instead and set IsReadOnly = true or IsEnabled = false
Related
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.
How do I remove the light gray color that appears behind the text of the disabled checkboxes? Thanks in advance!
I have changed ALL system colors:
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ActiveBorderBrushKey}" Color="Transparent"/>
...
</Style.Resources>
but the background color behind text (when checkbox is Disabled) remains unchanged.
Setting Focusable="False" and IsHitTestVisible="False" will "disable" the checkbox without changing the appearance of it.
Not sure what you mean by "behind the text." The text itself becomes gray, but you can easily change the behavior using a Trigger:
<CheckBox Content="CheckBox" IsEnabled="False">
<CheckBox.Style>
<Style TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
Obviously a very simplified example here.
I know this is a really old question, but googling still finds it as one of the top results.
In this situation, you can just set IsHitTestVisible = false instead of IsEnabled = false.
This will display the checkbox as enabled while preventing any clicks within it.
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
I was trying out some dummy application just to test binding modes. So, just curious to know how did the binding modes work. I have this xaml code-
<Button x:Name="btn"
Height="20"
Width="200"
VerticalAlignment="Top">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="IsEnabled"
Value="{Binding CanEnable, Mode=TwoWay}" />
<Style.Triggers>
<DataTrigger Binding="{Binding TextChanged}" Value="true">
<Setter Property="IsEnabled"
Value="true" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Here by button IsEanbled is binded to my viemodel property "CanEanble" whose default value is false. Now in my trigger i was listening to "TextChanged" property and setting button IsEnabled to true. Button gets enabled as it should be but the property "CanEnable" did not set to true even the biding mode is set to TwoWay..
Why this is happening??
By setting the value in the trigger you basically remove the binding you previously set in the style setter. Take a closer look at the style. You will notice that you basically you set the property IsEnabled twice. First in the style setter, second in the trigger. It is logical that the second value overrides the previous value.
The desired effect can be achieved from code if you set the value of the dependency property using SetCurrentValue method:
SetCurrentValue(Button.IsEnabledProperty, true);
This way the bindings set on this property will not be removed and it will work as expected.
I have a targeted WPF style:
<Style x:Key="ImgFadeStyle"
TargetType="Image">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" Value=".5" />
</Trigger>
</Style.Triggers>
</Style>
I apply it to some images:
<Image Name="imgFirst"
Grid.Column="0"
Style="{StaticResource ImgFadeStyle}"
Source="pack://application:,,,/AppGUI;component/Images/Temp/resultset_first.png"
ToolTip="First"
MouseLeftButtonUp="imgFirst_MouseLeftButtonUp" />
<Image Name="imgLast"
Grid.Column="6"
Style="{StaticResource ImgFadeStyle}"
Source="pack://application:,,,/AppGUI;component/Images/Temp/resultset_last.png"
ToolTip="Last"
MouseLeftButtonUp="imgLast_MouseLeftButtonUp" />
In the codebehind, I set the IsEnabled property to "true" or "false". The first image always reacts as expected. The second image does not react to the property change. Does anyone know what is wrong?
Thanks.
This could possibly be an issue caused by Dependency Property value precedence, if you set the Opacity of the last image somewhere explicitly this might override the style's efforts.