Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Spacing between elements in stackpanel

I have a stackpanel with checkboxes. I cant seem to make same spacing between checkboxes with margin property

Can some1 tell me what am i doing wrong?

The code below gives me this:

http://www.shrani.si/f/1Y/M6/4eniAdAw/margin.png
(source: shrani.si)

As you can see, the spacing between elements is not constant !

<StackPanel MinWidth="150" cal:Bind.Model="{Binding}" Orientation="Horizontal">
    <StackPanel.Resources>
        <Style TargetType="{x:Type CheckBox}">
            <Setter Property="Margin" Value="0,0,20,0"/>
        </Style>
    </StackPanel.Resources>
    <CheckBox IsChecked="{Binding IsShown}" Content="{Binding ModuleName, Converter={StaticResource localizeModuleAndFunctionConverter}}" 
              cal:Message.Attach="[Event Click] = [Action FilterShownModuleFunctions]" />
</StackPanel>
like image 764
no9 Avatar asked Jul 19 '10 11:07

no9


1 Answers

I'm guessing your problem is your MinWidth="150" property. I think you've got a total of 5 stackpanels right now. You have 4 stack panels each holding their own checkbox. Then I'm assuming you've got a 5th stack panel holding your 4 stack panels.

If this is true... then the problem is that each of your checkboxes are in a stack panel of 150 width (minimum) but your third stack panel is larger than 150 because the text is so long that it must be larger to contain the entire text (plus your margin of 20).

Remove the MinWidth="150" and I think you will get a margin of 20 between the text of each checkbox. (if you want the even spacing between the actual boxes of the checkboxes, then you should keep your minwidth but make it large enough that it is at least as wide as the checkbox containing the longest text).

like image 83
Scott Avatar answered Nov 15 '22 06:11

Scott