StackPanel - 限制父ListBox的宽度

时间:2013-06-20 21:16:49

标签: c# wpf

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

列表框项是DataTemplate,但是出于本文的目的,我已将其作为listboxitem复制到ListBox中。

这都包含在页面中。

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

如何让我的文本块(具有真正长文本的文本块)只能与列表框本身一样宽? (它可以将页面的边缘附加到其中)

Width of child control should match width of parent container我已经尝试过这个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>

2 个答案:

答案 0 :(得分: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}

答案 1 :(得分:0)

好的,明白了。

这:

ScrollViewer.HorizontalScrollBarVisibility="Disabled"

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