0

我有一个列表框,我想在其中布局项目。我正在努力使堆栈面板仅与父对象一样宽。也就是说,我的列表项中有一个文本块,其中可能包含很多信息,我希望它根据父项的宽度进行换行或修剪(尚未确定)。

列表框项是一个 DataTemplate,但是出于本文的目的,我将其复制为列表框内的列表框项。

这都包含在一个页面中。

 <Grid>           
        <ListBox Name="ListBoxManageMedia" HorizontalContentAlignment="Stretch" Margin="10,52,10,41" ScrollViewer.VerticalScrollBarVisibility="Visible">        
            <ListBoxItem Height="70" Name="ListBoxItem" PreviewMouseDown="ListBoxItem_OnMouseDown">    
                <StackPanel Height="65" Orientation="Horizontal">
                    <StackPanel Name="VideoImage2" Height="65" Width="102"/>    
                        <StackPanel Name="VideoData2" HorizontalAlignment="Stretch" Height="65">
                            <TextBlock Text="Title" FontWeight="Bold" FontSize="18"></TextBlock>
                            <TextBlock TextTrimming="None" Text="long test text about something or nothing to demonstrate the long description issues I'm having. I want this to wraplong test text about something or nothing to demonstrate the long description issues I'm having. I want this to wrap" TextWrapping="Wrap"  FontSize="13" Margin="0,0,0,0"/>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Cat" FontSize="12" FontStyle="Italic"/>
                                <Border Width="50"></Border>
                                <TextBlock Text="Status" FontSize="12"/>
                            </StackPanel>
                        </StackPanel>                   
                </StackPanel>
            </ListBoxItem>    
        </ListBox>     
    </Grid>

我怎样才能让我的文本块(文本很长的那个)只与列表框本身一样宽?(它可以很好地“附加”到页面的边缘)

子控件的宽度应该与父容器的宽度相匹配我已经尝试过这个 SO 答案但没有成功,还有许多其他人喜欢它。

编辑:

即使作为网格,我的长文本也只是在页面外运行。我在这里做错了什么?

 <ListBoxItem Height="70" HorizontalContentAlignment="Stretch" Margin="0,0,10,0" Name="ListBoxItem" PreviewMouseDown="ListBoxItem_OnMouseDown">
                <Grid Height="65" Margin="0,0,0,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50*"/>
                        <ColumnDefinition Width="330*"/>
                        <ColumnDefinition Width="3*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="23"></RowDefinition>
                        <RowDefinition Height="23"/>
                        <RowDefinition Height="19"/>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Column="1" Grid.Row="0" Text="Title" FontWeight="Bold" FontSize="18"></TextBlock>
                    <TextBlock Grid.Column="1" Grid.Row="1" DockPanel.Dock="Top" TextTrimming="CharacterEllipsis" Text="long test text about something or nothing to demonstrate the long description issues I'm having. I want this to wraplong test text about something or nothing to demonstrate the long description issues I'm having. I want this to wrap" TextWrapping="NoWrap"  FontSize="13" Margin="0,0,0,0"/>
                    <StackPanel Grid.Column="1" Grid.Row="2" DockPanel.Dock="Top" Orientation="Horizontal">
                        <TextBlock Text="Cat" FontSize="12" FontStyle="Italic"/>
                        <Border Width="50"></Border>
                        <TextBlock Text="Status" FontSize="12"/>
                    </StackPanel>
                </Grid>
            </ListBoxItem>
4

2 回答 2

0
<TextBlock Grid.Column="1" 
 Width="{Binding ActualWidth, ElementName=ListBoxManageMedia}" Grid.Row="1"
DockPanel.Dock="Top" TextTrimming="CharacterEllipsis" 
Text="long test text about something or nothing to demonstrate the long 
description issues I'm having. I want this to wraplong test text about something 
or nothing to demonstrate the long description issues I'm having. I want this to wrap"
TextWrapping="NoWrap"  FontSize="13" Margin="0,0,0,0"/>

如果您希望 TextBlock 采用其父级的宽度,您可以像上面那样绑定宽度,但要确保它的父级在这种情况下说 Listbox 已定义其宽度。高度也是如此。Height={Binding ActualHeight, ElementName=ListBoxManageMedia}

于 2013-06-20T21:51:24.747 回答
0

好的,我知道了。

这个 :

ScrollViewer.HorizontalScrollBarVisibility="Disabled"

并将 StackPanels 转换为 DockPanels 就可以了。

于 2013-06-20T21:52:23.663 回答