Unchained Unchained - 1 month ago 9
C# Question

Set a specific height for ItemTemplate

I want set a specific height for the DropDownButton ItemTemplate, in particular, I've a lot of elements inside the DropDown Button and actually the list displayed is too long as you can see in the image below, the structure is this:

<Controls:DropDownButton Content="Nazioni" Width="120" Margin="0, 0, 20, 0"
ItemsSource="{Binding Countries}"
ItemTemplate="{StaticResource CombinedTemplate}"/>


enter image description here

Is possible set a specific height?

Answer

If you want to set the height of the element that is using the supplied ItemTemplate to display an item (this element is usually referred to as item container), you should use the ItemContainerStyle property (inherited from ItemsControl):

<Controls:DropDownButton (...)>
    <Controls:DropDownButton.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Height" Value="..." />
        </Style>
    </Controls.DropDownButton.ItemContainerStyle>
</Controls.DropDownButton>

EDIT

If you want to limit the height of the drop-down itself the way to go about it is to use the DropDownButton.MenuStyle property:

<Controls:DropDownButton (...)>
    <Controls:DropDownButton.MenuStyle>
        <Style TargetType="ContextMenu" BasedOn="{StaticResource {x:Type ContextMenu}}">
            <Setter Property="MaxHeight" Value="..." />
        </Style>
    </Controls.DropDownButton.MenuStyle>
</Controls.DropDownButton>
Comments