How do I know the active control in the XAML designer?

How do I know, at design time, which control is active / focused by the user in a custom item control to show the rendering for the selected item?

I am after functionality similar to TabControl :

enter image description here

The difference in my control is that it displays a very simple sequential workflow and shows breadcrumbs instead of tabs. Currently I am just showing the first content of the control panel. I need to know when a developer has another panel active in the XAML editor to display the content of that panel accordingly.

Currently I got a value that will be available in MeasureOverride

, but will be flexible if I have something available in the code.

I have tried hacks like

if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
    foreach (var panel in this.Panels)
    {
        panel.GotFocus += focusHandler;
    }
}

      

and using System.Windows.Input.FocusManager.GetFocusedElement()

, but still haven't met any luck.

+3


source to share


1 answer


There are two possible ways to achieve this:

The Hard Way (Visual Studio Designer Support)

Your best bet is to look at this to see the WPF Designer Extensibility documenation and also see how the DesignTime support for the TabControl is implemented.

At design time, a designer has a mechanism to prevent user interaction with controls. This is why it is difficult for you to determine which element has focus.

You can teach the Designer how to provide custom decorations in your control so that the user can interact with them through adorners.

For your reference, the TabControl extensibility modules can be found at:

C:\Program Files (x86)\Microsoft SDKs\Silverlight\v5.0\Libraries\Client\Design
- System.Windows.Controls.Design.dll
- System.Windows.Controls.VisualStudio.Design.dll
- System.Windows.Controls.Expression.Design.dll

      



Main idea:

  • SelectedTabItemPolicy

    lists the control types (in this case it is TabItem

    ) where the selection can be displayed
  • TabItemAdornerProvider

    then calls SetDesignTimeIsSelected

    on the selected TabItem
  • This, in turn, causes:

    item.get_Context () get_Services () Publishing (services); ..

which appears to accept a dictionary of values. There are many rabbit holes here.

Easier exit

In fact, you can react to changes in the property sheet. eg When SelectedIndex

changes, you can make different pages visible.

Changes Updated with links to WPF Designer documentation.

+1


source







All Articles