7

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.

Mouse cursor color comparison.

I know how to change four things:

  1. Textbox background color (.Background)
  2. Textbox foreground color (.Foreground)
  3. Textbox caret color (.CaretBrush)
  4. 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.

XSapien
  • 152
  • 2
  • 11

2 Answers2

8

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?

Community
  • 1
  • 1
ViVi
  • 4,339
  • 8
  • 29
  • 52
  • I can change that. I actually changed it to white. But it only affects the blinking cursor in the textbox, not the mouse over cursor. It would be nice if it changed both, that would have made the most sense. As it stands now, I lose track of my mouse cursor when it passes into the textbox because it's so hard to see. – XSapien May 06 '16 at 03:47
  • It is possible. But I haven't tried it yet. Give it a try anyways http://stackoverflow.com/questions/46805/custom-cursor-in-wpf – ViVi May 06 '16 at 04:08
  • But I am sure it will be bit hectic doing that, since you might need to define custom cursors for every different colored textbox. – ViVi May 06 '16 at 04:19
  • Ya, I was hoping since WPF can change the cursor color automatically for a black background, that there would be a manual way. But I may just have to settle for creating a custom .cur file. Apparently the cursor in question is the "Cursors.IBeam" cursor. Maybe I can just find a white one. – XSapien May 06 '16 at 04:52
  • Yup. It's a good way I guess. When i get free time, I will try creating a custom cursor. But it is not required in normal cases since all textboxes will be having the same style across the whole project. So CaretBrush change only is required most of the times. – ViVi May 06 '16 at 05:41
  • 1
    I edited the list of 4 things I can change in my original question. I added the properties associated with each thing I listed. Since I want to change the mouse cursor I have two options: OverrideCursor and Cursor. "The main difference is that Mouse.OverrideCursor will set the mouse cursor for the whole application while this.Cursor will set it only for that specific FrameworkElement." Example using available cursors: `Mouse.OverrideCursor = Cursors.IBeam;` `this.Cursor = Cursors.IBeam;` – XSapien May 06 '16 at 07:57
  • Using a trigger in XAML like in your edit is probably a better way to go then the C# route (e.g. `Mouse.OverrideCursor` or `this.Cursor`). Then the cursor can be added as a resource in XAML as well: `C:\Users\Images\IBeamWhite.cur\ `. I was hoping to find the .cur files for the standard cursors available in Visual Studio and just changing the color. But no such luck. – XSapien May 06 '16 at 08:45
  • Did you search the internet? I think you could get lots of samples for cursor files. Just need to modify it. Yes. It's right. The best thing to do in WPF is to reduce the code behind and to include majority of view related stuff in xaml itself. Keep coding :) – ViVi May 06 '16 at 14:07
1

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.