Hosting any control in a ToolStrip

When using ToolStrips, you soon discover that not many Classes of items can be added regularly.

There is a simple way of adding any kind of Control to a ToolStrip by using the ToolStripControlHost class:

ToolStrip ts = new ToolStrip();
ts.Items.Add( new ToolStripControlHost( new DateTimePicker()));

However, there is NO design-time support for this. jfo has described how to add design-time support on his blog. I have tried this with a DateTimePicker Control and experienced strange Designer behaviour (The Designer show DTP as a permitted Item Type but when I select it, it disappears immediately.

Another unexpected behaviour occurs in case of some third-party controls. If, for example, you add a Janus CalendarCombo to a ToolStripControlHost, the control vanishes and the ControlHost fails to show. Effectively, you've just lost a control :)

After some research on the Janus Website, I found out that you have to set the hosting element's AutoSize Property to False.

ToolStrip ts = new ToolStrip();
ToolStripControlHost tch = new ToolStripControlHost( new DateTimePicker())
tch.AutoSize= false;
ts.Items.Add( tch );


My assumption is, that the component in question delivers its dimension as 0,0 to the ControlHost, so that the ControlHost automatically shrinks to 0,0 with it and is, albeit present, not visible.

Kommentare

Beliebte Posts aus diesem Blog

WPF Diaries : Using HLSL pixel shaders

Getting the Crisp back into your WPF Apps

WPF and Workflow Foundation : XAML parser compatibility issues