Search code examples
c#wpfcenterstackpanel

stackpanel not center-aligning


I have a listbox with inside a various stackpanels. One is formed with a textblock and a combobox:

<ListBox x:Name="lb1Tab3" Visibility="Visible" HorizontalContentAlignment="Stretch" Height="1500" VerticalAlignment="Stretch" VerticalContentAlignment="Center"  FontSize="{StaticResource BUTTON_FONTSIZE}" Background="Transparent" BorderBrush="{x:Null}" >
  <StackPanel Name="sp1_lb1Tab3" Background="Red" Orientation="Horizontal"  VerticalAlignment="Center" Margin="0"  >
    <TextBlock x:Name="lbLanguage" Margin="20" HorizontalAlignment="Left"  VerticalAlignment="Center" Text="Language"/>
    <ComboBox x:Name="cmbLanguages" Margin="20" HorizontalAlignment="Left"  VerticalAlignment="Center" Width="246" Height="35" DropDownClosed="ComboBox_DropDownClosed"/>
  </StackPanel>

then, in code behind I set various dimensions

 int marginStackPanel = 40 * 2;
    int marginText = 40;
    int marginComboBox = 20;
    /*-------------------*/
    sp1_lb1Tab3.Height = easyRunData.FontSize + marginStackPanel;
    sp1_lb1Tab3.VerticalAlignment = VerticalAlignment.Center;
    lbLanguage.Height = easyRunData.FontSize + marginText;
    lbLanguage.FontSize = easyRunData.FontSize;

    cmbLanguages.Height = easyRunData.FontSize +marginComboBox ;
    cmbLanguages.FontSize = easyRunData.FontSize;

and I expect them to be vertically centered but the effect is:

enter image description here thanks for helping


Solution

  • int marginText = 40;
    lbLanguage.Height = easyRunData.FontSize + marginText;
    

    Since MarginText is 40 it increases the lbLanguage size to be bigger than the combobox and it aligns it higher. Probably try a lesser number. like int marginText = 25;