Search code examples
c#wpfpanel

Space inside Wrap panel


I creating buttons from db list, that's my .xaml file

<Controls:MetroWindow
    <ItemsControl x:Name="itemsList" Height="500" Width="550" >
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="local:Hall">

                <ToggleButton x:Name="ToggleButton"
                    Click="ItemButtonClick"
                    Width="35"
                    Height="35"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Margin="5">
                    <TextBlock TextWrapping="Wrap" TextAlignment="Center" Text="{Binding number}"/>
                </ToggleButton>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>

            <WrapPanel Margin="50" VerticalAlignment="Center" HorizontalAlignment="Center">
            </WrapPanel>
        </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Controls:MetroWindow>

Is it possible to achieve something like this below on the picture? I need space in this places between buttons

spacing inside


Solution

  • Maybe you can use a solution like this. You can have different values for numbers and return them accordingly. In your case something like this should work:

    Create a property in your class called e.g Margin.

    public Thickness Margin {
        get {
            int multiplier = 1;
            if (number == 2 || number == 8) multiplier = 2;
    
            return new Thickness(5, 5, 5 * multiplier, 5);
        }
    }
    

    In XAML:

    Margin="{Binding Margin}"