【问题标题】:StackPanel - Confine to the width of the parent ListBoxStackPanel - 限制在父 ListBox 的宽度
【发布时间】:2013-06-21 05:52:23
【问题描述】:

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

列表框项是一个 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>

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

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>

【问题讨论】:

  • 您真的需要StackPanel 来布置项目吗? Grid 会起作用吗?
  • 当我输入时我正在尝试,目前没有太大成功

标签: c# wpf


【解决方案1】:

好的,知道了。

这个:

ScrollViewer.HorizontalScrollBarVisibility="Disabled"

并将 StackPanels 转换为 DockPanels 就成功了。

【讨论】:

    【解决方案2】:
    <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}

    【讨论】:

    • 谢谢@Jack,我对绑定宽度没有兴趣。
    • 没问题。很高兴我能帮上忙。
    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    相关资源
    最近更新 更多