A
A
Anton2021-05-16 15:34:13
WPF
Anton, 2021-05-16 15:34:13

Why aren't sizes from styles applied to elements in a StackPanel?

Hello, I've run into a problem with the app.

I have styles with predefined sizes:

<Style x:Key="comboBox"
         TargetType="ComboBox">
    <Setter Property="Control.MinHeight"
            Value="15" />
    <Setter Property="Control.MaxHeight"
            Value="25" />
    <Setter Property="Control.MinWidth"
            Value="100" />
    <Setter Property="Control.MaxWidth"
            Value="250" />
  </Style>

  <Style x:Key="textBox" 
         TargetType="TextBox">
    <Setter Property="Control.MinHeight"
            Value="15" />
    <Setter Property="Control.MaxHeight"
            Value="25" />
    <Setter Property="Control.VerticalContentAlignment" Value="Center"/>
  </Style>


And there is a piece of layout:
<StackPanel Grid.ColumnSpan="2"
                VerticalAlignment="Center"
                Orientation="Horizontal"
                Margin="0 0 0 5">

      <StackPanel Margin="0 0 10 0">
        <TextBlock Text="Тип документа"
                          Margin="0 0 0 2" />
        <ComboBox Style="{StaticResource ResourceKey=comboBox}"/>
      </StackPanel>
      
      <StackPanel>
        <TextBlock Text="Категория документа"
                          Margin="0 0 0 2"/>
        <ComboBox Style="{StaticResource ResourceKey=comboBox}"/>
      </StackPanel>
      
    </StackPanel>


The bottom line is that when these comboboxes are inside a StackPanel, the sizes specified in the styles are not applied to them, but if I set the minimum width directly in the combobox, then the sizes are applied. How to fix such behavior?

I attached the style file to the application's shared resources:
<Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="Resources/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question