在WPF ListBox中限制项目高度,并带有指示器 [英] Constraining item heights in WPF ListBox, with indicator

查看:185
本文介绍了在WPF ListBox中限制项目高度,并带有指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中有一个ListBox控件,其中包含高度可变的项目(主要是大文本块,因此它也受自动换行的影响).由于当单个项目的高度过高时(特别是当其接近ListBox本身的高度时),滚动行为会变差.因此,我想限制单个项目的最大高度.

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.

通过使用样式设置ListBoxItem容器的MaxHeight,我已经很容易做到这一点.

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.

这是我的第一次尝试:

<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.

推荐答案

尝试下面的代码.我将ListBoxItem.MaxHeight设置为99.然后在DataTemplate中添加了一个触发器,用于检查模板中根元素的ActualHeight(即下面的"bd"),如果它是99,则更改BorderBrush.希望这会有所帮助.

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>

这篇关于在WPF ListBox中限制项目高度,并带有指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆