0

I had made a user control that is a replacement for the combo box, it has search and LINQ queries capabilities.

The control is made by composition, it has a TextBox where the user type, a TextBlock that serves as the auto-complete label, a ListBox inside a Popup for the drop down and the drop down button.

The problem is that each of this controls has its own focus, and when a consumer of this user control calls the SetFocus() for the user control, the focus is not setted to the TextBox, even if I subscribe to the GotFocus event, this is because the user control itself is not focusable.

If instead I set the IsFocusable = True and KeyboardNavigation.IsTabStop = True the user control itself can be focused, but the text box its not focused, instead, a dotted line appears around the control

I need that the user control behave the same way as the native ComboBox, when a consumer calls the SetFocus() the inner TextBox should be focusd

I could expose a custom method for focusing the inner TextBox, but this is not an option because I have an attached property (from this question) that allows me to set the focus of any control from the ViewModel, thus the focus behaviour should be the same for all controls

Community
  • 1
  • 1
Rafael
  • 2,642
  • 2
  • 24
  • 30
  • 1
    There are multiple problems with your approach: 1 - a `UserControl` is not intended for what you did here, a *custom control* is. Start reading [here](https://msdn.microsoft.com/en-us/library/vstudio/ms745025(v=vs.100).aspx). 2 - anything related to LINQ is a data concern and is not a responsibility of the View. Should be done by a proper ViewModel instead. 3 - you should have derived from `Selector` if you're going to select items. – Federico Berasategui Sep 04 '15 at 18:47
  • Also, since you derived from `UserControl`, which in turn is a `ContentControl`, what happens if I do `` and put `SomeElement` inside of your UserControl in XAML? it's either not going to work, or blow up. – Federico Berasategui Sep 04 '15 at 18:49
  • For reference, I created my own LookupBox with similar capabilities, and it consists of a (code-only, no XAML) class derived from `Selector`, and a default (generic.xaml) template for that. – Federico Berasategui Sep 04 '15 at 18:51
  • Thanks, I will read about custom controls, it is the correct approach. @HighCore Why anything related to LINQ is data concern? If a control already depends on IEnumerable, can't depend also on IQueryable ? This is because i want that, if the ItemsSource supports it, that the search is done via the LINQ Where statement, instead of a foreach or IEnumerable Where search – Rafael Sep 04 '15 at 18:55

0 Answers0