Search code examples
c#.netwpfvisual-studio-2022

How to reduce the margin between two buttons in below WPF code


enter image description hereI tried all the ways to reduce space between two buttons named Analyze and ShowMap. But I am getting as the above attached image. Code is as below

 <Grid>
     <Grid.ColumnDefinitions>
         <ColumnDefinition Width="*" />
         <ColumnDefinition Width="*" />
         <ColumnDefinition Width="*" />
     </Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
         <RowDefinition Height="Auto" />
         <RowDefinition Height="Auto" />
         <RowDefinition Height="Auto" />
         <RowDefinition Height="*" />
     </Grid.RowDefinitions>
     <Label Content="Plot File" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10" />
     <TextBox x:Name="txtMultiBox" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="10,0,10,10" TextWrapping="Wrap" AcceptsReturn="True"/>
     <Button Style="{StaticResource Highlight}"   Content="Browse" x:Name="btnBrowse" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10" Click="btnBrowse_Click" />
     <Button Style="{StaticResource Highlight}"  Content="Analyze" x:Name="btnAnalyze" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="left" Margin="10,10,5,7.5" Click="btnAnalyze_Click" />
     <Button Style="{StaticResource Highlight}" Visibility="Hidden"  Content="ShowMap" x:Name="btnShowmap" Grid.Column="2" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="left" Margin="5,10,10,10"  Click="btnShowmap_Click" />
     <Label Content="Parcels" Grid.Column="0" Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,7.5,10,0" />
     <TreeView SelectedItemChanged="tvSections_SelectedItemChanged"  x:Name="tvSections" Grid.Column="0" Grid.Row="4" Margin="10,0,10,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
     <Label Content="Assosiated Sections" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,7.5,10,0" />
     <ListView x:Name="lsParcels" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="4" Margin="10,0,10,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
         <ListView.View>
             <GridView>
                 <GridViewColumn Header="FID"  DisplayMemberBinding="{Binding Fid}"/>
                 <GridViewColumn Header="ParcelId"  DisplayMemberBinding="{Binding ParcelId}"/>
             </GridView>
         </ListView.View>
     </ListView>
 </Grid>

Solution

  • One possible solution would be to wrap the two button in StackPanel with Orientation="Horizontal", occupying the same place in the Grid as the buttons:

    <StackPanel Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Orientation="Horizontal">
        <Button Style="{StaticResource Highlight}"  Content="Analyze" x:Name="btnAnalyze" VerticalAlignment="Center" HorizontalAlignment="left" Margin="10,10,5,7.5" Click="btnAnalyze_Click" />
        <Button Style="{StaticResource Highlight}" Visibility="Hidden"  Content="ShowMap" x:Name="btnShowmap" VerticalAlignment="Center" HorizontalAlignment="left" Margin="5,10,10,10"  Click="btnShowmap_Click" />
    </StackPanel>