pingvinius
pingvinius

Reputation: 421

The strange internal indent of WPF WrapPanel

I have WPF WrapPanel, and a toolbar inside. The WrapPanel is red and the toolbar is blue.

enter image description here

Could anybody explain me, where did indent of WrapPanel on the top, left etc. come from, and how I can remove it?

Here is the sample code of the image:

<WrapPanel Orientation="Horizontal" Background="Red">
       <ToolBar x:Name="tFirst" Background="Blue" ToolBarTray.IsLocked="True">
         <Button ToolTip="New" Content="New" />
         <Button ToolTip="Save" Content="Save" />
         <Button ToolTip="Delete" Content="Delete" />
       </ToolBar>
</WrapPanel>

Thanks in advance.

UPDATE: the problem is only with Toolbar, I just changed WrapPanel to DockPanel, and the problem is still exists.

Upvotes: 0

Views: 527

Answers (1)

Konrad Morawski
Konrad Morawski

Reputation: 8394

It's probably something about the default, built-in control template of the ToolBar control.

A quick fix is, obviously, to set Margin to -2 for example.

ToolBar is internally quite complex, it consists of a Grid, a Border, a DockPanel etc. you can try and traverse it with VisualTreeHelper. For example:

var dockPanel = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(tFirst, 0), 1), 0);

retrieves the DockPanel object. You can try and find the control with Margin or Padding greater than 0, and if you succeed you could probably even reset it from code (something like: (dockPanel as DockPanel).Margin = 0;)

In XAML, I'd try overriding ToolBar's control template (as demonstrated in http://msdn.microsoft.com/en-us/library/aa970772(v=vs.85).aspx - although this example is of course overly complex for your purpose, the principle is the same).

Upvotes: 1

Related Questions