10129

How to add a focus style to an editable ComboBox in WPF

I've been looking at the following example on how to style the ComboBox, but I haven't been able to create a focus effect when going into an editable combo box. Whenever the ComboBox receives focus, it should go into edit mode and the component should have a focus style.

The basic problem is that whenever I go into the edit mode, it's not the surrounding ComboBox which actually has the focus, but the text subcomponent and I haven't been able to create a Trigger on the text component which modifies the ComboBox's border style since I don't know how to refer to the parent component from the trigger.

I've tried adding ControlTemplate Trigger on the TextBox, or style trigger. I've tried to refer to the ComboBox by name or by using the TemplateBinding option, but without any luck. A simple example would be very appreciated.

Answer1:

Bind IsKeyboardFocusWithin to IsDropDownOpen

<ComboBox ItemsSource="{Binding SortedItems}" StaysOpenOnEdit="True" IsDropDownOpen="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}, Mode=OneWay}" />

Answer2:

private void cmbSpecialHandling_GotFocus(object sender, RoutedEventArgs e) { Thickness th = new Thickness(2); cmbSpecialHandling.BorderThickness = th; cmbSpecialHandling.BorderBrush = this.FindResource("TabFocusColor") as SolidColorBrush; } private void cmbSpecialHandling_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { Thickness th = new Thickness(2); cmbSpecialHandling.BorderThickness = th; cmbSpecialHandling.BorderBrush = this.FindResource("TabFocusColor") as SolidColorBrush; } private void cmbSpecialHandling_LostFocus(object sender, RoutedEventArgs e) { cmbSpecialHandling.BorderBrush = Brushes.Transparent; }

Answer3:

Set the border brush of combobox in its Gotfocus and make it transparent in lost focus:

private void comboBox_GotFocus(object sender, RoutedEventArgs e) { Thickness th = new Thickness(2); comboBox.BorderThickness = th; comboBox.BorderBrush = this.FindResource("TabFocusColor") as SolidColorBrush; or comboBox.BorderBrush = Brushes.Green; } private void comboBox_LostFocus(object sender, RoutedEventArgs e) { comboBox.BorderBrush = Brushes.Transparent; }

Recommend

  • WPF - No IsEnabled=true behavior using style with animations
  • ListBox selection state Customization
  • How to insert an image list in the grouped items page befor each group?
  • Local WPF C# Programming
  • Find where element is selected in source
  • C++ Concurrent modification and read of a single global variable
  • How can I bind a string to double in WPF?
  • DropShadowPanel adapt to button template style
  • How to create a fast loading wrapping ListBox?
  • Confusing prototype behavior in JavaScript
  • Creating a SOAP proxy?
  • TFS Builds: Running the builds as administrator
  • Ajax Form Submit with submit button
  • Apply PivotItemHeader style to PivotItem in UWP
  • c# How to detect and process a .URL file type on DragDrop event
  • Hudson dependencies
  • Disable div click on Ajax start and re-enable it on Ajax complete
  • Having an issue with my TextBox control template
  • 2-table interaction: insert, get result, insert
  • How to save dynamically created textboxes and their values
  • What's wrong with my datatrigger binding?
  • How to add closing tag for canvas in three js rendered Canvas?
  • Git describe fails to return most recent annotated tag
  • SonarQube: Cannot deactivate rule with missing quality profile
  • How do I include a SWC in an AS2 Flash project?
  • Web.config system.webserver errors
  • Force show.bind execution
  • How do I superscript characters in a UIButton?
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • Disable Enter in editText android
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Email format validation in mvc3 view
  • Change JButton Shape while respecting Look And Feel
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • javascript inside java/jsp code
  • jquery mobile loadPage not working
  • WPF Applying a trigger on binding failure
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?