簡體   English   中英

ListBox自定義ItemTemlate,WP8,C#,WPF中StackPanel的動態高度

[英]Dynamic height of StackPanel in ListBox custom ItemTemlate, WP8, C#, WPF

我正在編程的應用程序將顯示RSS提要中的數據。 我只需要很少的視圖即可顯示較小的圖像和描述。 我有一個stackPanel的自定義ListBox,其中and是。 問題是:該列表框中的每個項目都有一定的高度,但是我需要該高度根據文本的長度和圖像的大小動態變化。

這是我的ListBox:

<ListBox x:Name="gui_listNovinky" SelectedIndex="-1" Height="500" VerticalAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Hidden" HorizontalContentAlignment="Stretch" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="0,1,0,0" BorderBrush="Black" Margin="10">
                <StackPanel Name="stackpanel" Orientation="Horizontal" Height="500">
                    <StackPanel Width="435" Height="1000">
                        <Image Source="{Binding Image}" Height="200" Width="230" VerticalAlignment="Top"/>

                        <TextBlock Text="{Binding Description}" MaxHeight="290" FontSize="20" VerticalAlignment="Top" Foreground="Black" TextWrapping="Wrap" />
                    </StackPanel>
                </StackPanel>
            </Border>
        </DataTemplate>

    </ListBox.ItemTemplate>
</ListBox>

看起來像這樣。 您會看到圖像和文本下方令人討厭的自由空間。 這么難看... :(

img

我將非常感謝您的解決方案。 :)

PS:如果我的英語有誤,不要害怕問,我會解釋的。 :)

不要在數據模板中強制使用大小。 修改后的DataTemplate將解決問題。

<DataTemplate>
    <Border BorderThickness="0,1,0,0"
            BorderBrush="Black"
            Margin="10">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Image Source="{Binding Image}"
                    Height="200"
                    Width="230"
                    VerticalAlignment="Top" />
            <TextBlock Text="{Binding Description}"
                        MaxHeight="290"
                        FontSize="20"
                        Grid.Row="1"
                        VerticalAlignment="Top"
                        Foreground="Black"
                        TextWrapping="Wrap" />
        </Grid>
    </Border>
</DataTemplate>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM