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