Setting the color of the blinking cursor in an editable ComboBox

When setting the foreground and background of the 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 you do the same for the 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> <ComboBoxItem>Test2</ComboBoxItem> </ComboBox> 

So, how do I set the flashing color of the ComboBox cursor?

+4
source share
2 answers

There is a way to change the color of the carriage by redoing the text box. The cartridge does not blink, changing the color between black and white, but changing its color between the background color and the XOR value of the background color (the first paragraph in the "Additional Information" section here better explains what windows do to make the carriage blink). This applies to its own text field and should be applied to any β€œcustom” written carriage in order to maintain look'n'el compliance with Windows standards.

There is a small workaround for WPF with which you can change the caret color:

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

This way you set the background color (yellow), the carriage will blink between that color and yellow XOR (blue), but the yellow background will never be displayed (since the template does not care about the background color). (the above code is just an example to show what I mean, it does not contain all the visual effects of a regular text field, but they can be easily added).

+6
source

Another option is to use the same bindings in 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 did not work for me, and I do not have enough time / experience to understand why, but it works fine.

0
source

Source: https://habr.com/ru/post/1334902/


All Articles