Deriving UIElement - how to receive focus - wpf

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.

Converting my comment into an answer:
I think you'd probably be better off deriving from FrameworkElement instead.

Related

disable all input controls in a WPF Window

Is there a way to make an entire WPF Window inert after a button click?
The window is invoked via Window.ShowDialog() and after use, the window is no longer needed for interaction but I leave it open to remind the user of the inputs in TextBox's, ListBox's, and give visual feedback via OxyPlot and so on. I leave it to the user to close the window manually.
One solution is to disable all buttons but that's tedious and it still leaves TextBox's functioning. That's not optimal because for anything to be functioning creates the wrong impression that the Window remains for anything other than looking at. It would be better for every control to be non-functioning by a single setting.
I did it by putting a name on the WPF window code behind and then setting .IsEnabled false upon the appropriate button click. All buttons, combo boxes, text boxes, and even OxyPlot became inert at that point and most parts were greyed out.
Consider creating a dedicated boolean dependency property in your code-behind or viewmodel and binding IsEnabled of every TextBox to the property.

Mimicing the tab key being pressed in Silverlight TextBox KeyDown event

Title pretty much explains it all really. I need to know how to automatically jump to the next element (the element that would get focus if the user presses the "Tab" key), whatever that may be, in the KeyDown event of a Silverlight TextBox. My TextBoxes are generated dynamically so I can't, for example, manually code TextBox1 to set focus on TextBox2 on keydown etc. Any help would be greatly appreciated
I don't believe there is an easy way to accomplish this. The only method I know of is to use the VisualTreeHelper and get all the elements and then loop through them by their tab stop values.
If your textboxes are generated dynamically you could use a Behavior to set their tab stop properties pretty easily which would be a much better solution. Or just set it when you create them.

Tooltip adorner

I am using a third party charting library (Infragistics xamChart). I am not satisfied with how tooltips are displayed on a line charts datapoints so I was thinking of using an adorner to make a better looking/interactive tooltip.
What I want to do is have the adorner popup whenever one of the datapoints is hovered over. Does anyone have any pointers on how I can do this? Most of the adorner samples I found are pretty simple ones that just alter the entire control they are adorning.
My main questions are:
Is it possible for an adorner to
appear only when certain elements are
hovered over (and have it appear at
the current cursor position)?
Does anyone have a simple sample they
can share?
Are adorners the right way to go
here? I think the only other option
was to use the popup control but I
though adorners were a nicer
solution.
Thank you.
Perhaps Attached Behaviours could help you? From this article:
The idea is that you set an attached
property on an element so that you can
gain access to the element from the
class that exposes the attached
property. Once that class has access
to the element, it can hook events on
it.
That way you could attach a listener to the MouseEnter event and display your custom ToolTip from there.

WPF: ArrangeOverride in Customized StackPanel

I want to make a custom Panel to (amongst other things) insert spacing between all children. To inherit from Panel seemed very complicated and perhaps unnecessary. What I want is close to StackPanel so I figured I could inherit from StackPanel and modify the ArrangeOverride and MeasureOverride to get what I want.
I got the implementation of these from Reflector but immediately noticed that one property was not accessible at all from a inherited class, namely IsScrolling (and the field _scrollData). My question is if I can skip this or if there is any other way of implementing identical behaviour? Im not sure I will need scrolling for this custom panel but I may do some day and I dont want to paint myself into a corner...
if all you want is just that all elements inside a container would automatically contain margin definitions, you're much better off just creating a custom behavior that implements that functionality.
to create a custom behavior, just implement Behavior<Panel>. It's rather self-explanatory, but if you don't know how then ask.

WPF How to get a UserControl to inherit a Button?

I created a UserControl which consists of a couple ellipses and labels. I added it just fine to another form and things were looking pretty snazzy.
Then I went to start adding some event handlers and discovered that the control I made did not expose a Click event. Oops. Easy to fix, right? Just go back to the code-behind on the UserControl I made and let it inherit Button.
Unfortunately, doing this caused a "Partial declarations of MyControl must not specify different base classes" message. This came as a surprise since I haven't declared any other base classes. Searching for the Partial in question returned no results either.
Is there a way to defeat this issue? If not, what would be the easiest workaround here? My goal is simply to get a Click event on the UserControl.
Yes, you've declared other base classes :) Just trust compiler :)
When you writing <UserControl ...></UserControl> in XAML, you are subclassing UserControl. If you want to subclass a Button, use <Button ...></Button> instead and ": Button" in code-behind file.
But I'm strongly encourage you to not subclass Button, its an overkill for you task. You can use MouseLeftButtonUp event instead of the Click event.

Resources