Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1074615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:09:21+00:00 2026-05-16T21:09:21+00:00

I have a ListBox control in WPF which contains items of variable height (predominantly

  • 0

I have a ListBox control in WPF which contains items of variable height (predominantly a large text block, so it’s also affected by word wrapping). Since scrolling behaves badly when the height of an individual item gets too high (especially when close to the height of the ListBox itself), I want to constrain the max height of the individual items.

I’ve done that readily enough, by using a Style to set the MaxHeight of the ListBoxItem container.

My problem is that I would like to detect that an individual item has hit that constraint, and then style it differently.

This was my first attempt:

<Style x:Key="LogContainerStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="MaxHeight" Value="64" />
    <EventSetter Event="MouseDoubleClick" Handler="LogEntry_MouseDoubleClick" />
</Style>
<DataTemplate x:Key="LogTemplate">
    <Grid>
        <TextBlock Text="{Binding Message}" />
        <TextBlock x:Name="More" Text="(more)"
                   HorizontalAlignment="Right" VerticalAlignment="Bottom"
                   Foreground="DarkGray" Visibility="Collapsed" />
    </Grid>
    <DataTemplate.Triggers>
        <Trigger ... height capped at MaxHeight? ...>
            <Setter TargetName="More" Property="Visibility" Value="Visible" />
        </Trigger>
    </DataTemplate.Triggers>
</DataTemplate>

But I’m not sure how to write the trigger. Alternatives welcome.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:09:22+00:00Added an answer on May 16, 2026 at 9:09 pm

    Try the code below. I set the ListBoxItem.MaxHeight to 99. I then added a trigger in the DataTemplate that checks the ActualHeight of the root element in the template (i.e. “bd” below) and if it’s 99, I change the BorderBrush. Hope this helps.

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication1"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            ShowActivated="False"
            Title="MainWindow" Height="350" Width="525">
        <ListBox x:Name="lb">
            <ListBox.ItemsSource>
                <x:Array Type="{x:Type sys:Double}">
                    <sys:Double>250</sys:Double>
                    <sys:Double>100</sys:Double>
                    <sys:Double>50</sys:Double>
                    <sys:Double>25</sys:Double>
                    <sys:Double>99</sys:Double>
                    <sys:Double>120</sys:Double>
                </x:Array>
            </ListBox.ItemsSource>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="MaxHeight" Value="99"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border x:Name="bd" BorderBrush="Black" BorderThickness="1">
                        <TextBlock Text="{Binding}" Height="{Binding}" Background="LightGray"/>
                    </Border>
                    <DataTemplate.Triggers>
                        <Trigger SourceName="bd" Property="ActualHeight" Value="99">
                            <Setter TargetName="bd" Property="BorderBrush" Value="Red"/>
                        </Trigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Within a Listbox control I have a Data Template which consists of text and
I have a WPF listbox with custom items. Each item is a user control
I have created a listbox control with following DataTemplate <DataTemplate x:Key=lb_Itemtemplate> <DockPanel> <TextBlock Text={Binding}
Is it possible to have a listbox displaying its items with a control template
I have a list box control that contains enough items to list them with
I have a WPF listbox control that is declaratively bound to a textbox. The
I have a WPF ListBox control and I'm setting its ItemsSource to a collection
I have a WPF Popup control with a ListBox and a Button in it.
I have a WPF Tab Control. Inside of it is a Listbox filled with
I have a WPF window which has a ItemsControl which contains list of User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.