1

我有一个列表框,我从一些收藏中填写。每个集合项目都有一些信息:静态名称和图片,以及动态内容(文本、照片或照片等)。我想为每个集合项目更改列表框的项目高度。高度取决于内容,如果它只有文本 - 列表框的项目必须具有静态(名称的文本块,图片的图像)和带有文本的文本块,并且文本块的高度必须根据文本改变(如果它是 2-3 个字符串,高度将为最小,如果文本适合 15-20 个字符串 - 文本块的高度必须根据字符串改变),如果集合项包括文本和图像 - 它必须具有静态(名称的文本块,图片的图像),文本的文本块和图像为图像。列表框项目的高度必须取决于文本块大小和图像大小。

此时,我有一个静态项目高度的列表框,并且想要使高度动态。

这是我的列表框项目之一,它有一个 ptoto

<local:NewsTemplateSelector.Photo>
    <DataTemplate>
        <Border BorderBrush="Red" BorderThickness="2"  Width="400" Height="auto" Margin="10">
            <StackPanel Orientation="Horizontal" Width="400" Height="auto">
                <Image Source="{Binding SourceImage}" Height="75" Width="75" Margin="0,-225,0,0" />
                <Canvas Width="400">
                    <TextBlock Text="{Binding SourceName}" Foreground="Black" FontSize="25" TextWrapping="Wrap" Height="55" Width="326" d:LayoutOverrides="VerticalAlignment, Height" />
                    <Image  Source="{Binding Photo[0].Big}" Height="auto" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="326" Canvas.Top="69"/>
                </Canvas>
            </StackPanel>
        </Border>
    </DataTemplate>
</local:NewsTemplateSelector.Photo> 

这是带有文本的模板

<local:NewsTemplateSelector.Texts>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" Background="YellowGreen" Width="400" Height="300" >
            <Image Source="{Binding SourceImage}" Height="75" Width="75" Margin="0,-225,0,0" />
            <Canvas Width="400">
                <TextBlock Text="{Binding SourceName}" FontSize="25" Foreground="Black" TextWrapping="Wrap" Width="326" Height="65" d:LayoutOverrides="VerticalAlignment, Height"  />
                <TextBlock Text="{Binding Texts}"  Foreground="Black" FontSize="25" TextWrapping="Wrap" Height="229" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="326" Canvas.Top="69" />
            </Canvas>

        </StackPanel>
    </DataTemplate>
</local:NewsTemplateSelector.Texts>

我尝试使用自动 - 但项目的高度为 0。如果我在列表框的项目下使用字段颜色为“红色”的边框 - 我会看到每个项目的红色线,而不是项目的内容

4

1 回答 1

0

请参阅本教程,了解如何使用模板选择器。http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector

于 2012-12-04T02:52:02.577 回答