0

I created a class based on UIElement and my intention is to render it myself overriding OnRender. Rendering works fine. Next I want to implement focus management and continue with other aspects of LIFE, but overriding GotFocus and calling Me.Focus() in it don't do a single thing. I places my control on a Window with one another control - TextBox, and clicking on it doesn't do a single think. Tab doesn't set focus too, and TextBox is AcceptsTab negative. I know I will have to visualize focus somehow in OnRender to actually tell when the control is focused or not, but first I need to allow it to receive focus and that's where I struggle. Could you please help me out?

P.S. I tagged this with FrameworkElement because I don't have enough reputation to create a tag UIElement and leaving tags empty seemed like a silly thing to do.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
  • `I created a class based on UIElement and my intention is to render it myself overriding OnRender. ` - Are you COMPLETELY sure you need this?? what is your control like that it can't be implemented using `Templates` and `Styles`? – Federico Berasategui Mar 14 '13 at 20:43
  • Oh, I am, my control will render text, but I can't even derive TextBox, because I want not only to render some parts of text differently, but also move them around, which is unsolvable using TextBox. – user2171565 Mar 14 '13 at 20:44
  • I think you'd probably be better off deriving from `FrameworkElement` instead. – Federico Berasategui Mar 14 '13 at 20:45
  • That helped, now my control will receive focus when I call Focus() in OnGotFocus() override. Thank you! But ugly focus cues are shown, is there a way to bypass this? – user2171565 Mar 14 '13 at 20:48
  • See [`FocusVisualStyle`](http://msdn.microsoft.com/en-us/library/ms744790.aspx) – Federico Berasategui Mar 14 '13 at 20:49

1 Answers1

1

Converting my comment into an answer:

I think you'd probably be better off deriving from FrameworkElement instead.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • This is the right answer, thanks again. However, FrameworkElement doesn't provide templating capabilities, they come with Control -- with many other things like Foreground and FontSize, that I don't want my Control to inherit. So setting a FocusVisualStyle is not a way to get rid of dashed rectangle. Neither is removing the call to base class in OnRender override. – user2171565 Mar 14 '13 at 21:17
  • Ok, I figured it out. Setting null to FocusVisualStyle didn't help, but setting an empty style did. All you need to watch out for is setting style's TargetType to Control, even tho your control derives from FrameworkElement: FocusVisualStyleProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(new Style() { TargetType = typeof(Control) })); – user2171565 Mar 14 '13 at 21:24
  • Yep, but the `FocusVisualStyle` is referring to the `FocusVisual`, which is a `Control`, not the `FrameworkElement` itself, that's why you must `TargetType="Control"`. – Federico Berasategui Mar 14 '13 at 21:26
  • Makes sense, love to learn new stuff! Thanks for helping me out. – user2171565 Mar 14 '13 at 21:28