Setting color of blinking cursor in editable ComboBox
When setting the foreground and background of a TextBox the color of the blinking cursor is automatically set. The code below will show a white cursor.
<TextBox Background="Black" Foreground="White">Test</TextBox>
When doing the same thing for an editable ComboBox the cursor color is not set. The code below will show a black (in this case invisible) cursor.
<ComboBox Background="Black" Foreground="White" IsEditable="True">
<ComboBoxItem>Test1</ComboBoxItem>
<Combo开发者_如何学GoBoxItem>Test2</ComboBoxItem>
</ComboBox>
So, how do I set the blinking cursor color of the ComboBox?
There is a way to change the color of the caret by retemplating the textbox. The caret blinks not by changing its color between black and white, but by changing its color between the background color and the XOR value of the background color (first paragraph in the More Info section here explains better what windows does to blink the caret). This applies to the native textbox and should apply to any "custom" written caret in order the keep the look'n'feel consistent with the Windows standards.
For WPF there is a small workaround with which one could change the color of the caret:
<TextBox Background="Yellow">
<TextBox.Template>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="Border">
<ScrollViewer Margin="0" x:Name="PART_ContentHost" Style="{DynamicResource SimpleTextScrollViewer}" />
</Border>
</ControlTemplate>
</TextBox.Template>
</TextBox>
In this way you set the background color (yellow), the caret will blink between this color and the XOR yellow (blue), but the yellow background will never be rendered (because the template doesn't care about the Background color). (the code above is just an example to show what I mean, it doesn't contain all the visuals of a normal textbox, but those can be added easy).
Another option is to use the same binding on the TextBox.
public override void OnApplyTemplate()
{
try
{
base.OnApplyTemplate();
myCombo.ApplyTemplate();
TextBox tb = myCombo.Template.FindName("PART_EditableTextBox", myCombo) as TextBox;
if (tb != null)
{
tb.SetBinding(TextBox.BackgroundProperty, myCombo.GetBindingExpression(ComboBox.BackgroundProperty).ParentBindingBase);
}
else
{
/* etc. */
}
}
catch (Exception) { /* etc. */}
}
The accepted answer wasn't working for me and I lack the time/expertise to figure out why, but this is working fine.
From: https://www.codeproject.com/Articles/633935/Customizing-the-Caret-of-a-WPF-TextBox You can just set the CaretBrush MSDN.
I fixed the problem in my ComboBox template, in the PART_EditableTextBox
<SolidColorBrush x:Key="DefaultTextBrush" Color="Pink"/>
<TextBox x:Name="PART_EditableTextBox"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"
Margin="{TemplateBinding Padding}" Style="{StaticResource ComboBoxEditableTextBox}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Foreground="{DynamicResource DefaultTextBrush}"
CaretBrush="{DynamicResource DefaultTextBrush}" />
Of course, you can omit the extra Brush resource.
精彩评论