wpf 项目宽度跟随控件宽度的列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13513957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:19:52  来源:igfitidea点击:

listbox with items width following control width

wpfuser-controlslistboxgriddatatemplate

提问by Marius Bancila

I'm building a WPF user control that contains a list whose items are displayed as an icon and a text. However, the texts have various lengths and I want them to scroll horizontally. Instead, I want the items have the same width as the user control, and the text be displayed wrapped (therefore with a vertical scroll).

我正在构建一个 WPF 用户控件,其中包含一个列表,其项目显示为图标和文本。但是,文本有各种长度,我希望它们水平滚动。相反,我希望项目具有与用户控件相同的宽度,并且文本显示为环绕(因此具有垂直滚动)。

Here is my XAML

这是我的 XAML

<UserControl x:Class="Demo.NotificationsWidget"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Border CornerRadius="2" 
           BorderThickness="1" BorderBrush="LightGray"
           Background="White">
      <DockPanel LastChildFill="True">
         <TextBlock Text="Alerts" DockPanel.Dock="Top" Margin="5" FontSize="15"/>

         <ListBox Name="alertsList" DockPanel.Dock="Bottom" Margin="5"
               Grid.IsSharedSizeScope="True"
               HorizontalContentAlignment="Stretch"
               BorderThickness="0" 
               ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
               <DataTemplate>
                  <Border BorderThickness="0,1,0,0" BorderBrush="Gray" Margin="5,0,5,5">
                     <Grid>
                        <Grid.ColumnDefinitions>
                           <ColumnDefinition SharedSizeGroup="col1" Width="40" />
                           <ColumnDefinition SharedSizeGroup="col2" Width="*" />
                        </Grid.ColumnDefinitions>

                        <Image Grid.Column="0" Source="{Binding Image}" Width="24" Height="24" Margin="5" />
                        <StackPanel Grid.Column="1" Orientation="Vertical" Margin="5">
                           <TextBlock Text="{Binding Title}" TextWrapping="Wrap" FontWeight="Bold" />
                           <TextBlock Text="{Binding Text}" TextWrapping="Wrap" />
                        </StackPanel>
                     </Grid>
                  </Border>
               </DataTemplate>
            </ListBox.ItemTemplate>
         </ListBox>
      </DockPanel>
   </Border>
</UserControl>

Here is an image with a window containing this user control. Notice that the control width varies with the window width.

这是一个带有包含此用户控件的窗口的图像。请注意,控件宽度随窗口宽度而变化。

enter image description here

在此处输入图片说明

What I want is than when the items width would exceed the list width, the text is wrapped (and I get a vertical scroll for the list).

我想要的是当项目宽度超过列表宽度时,文本被换行(并且我得到列表的垂直滚动)。

I have tried adding ScrollViewer.HorizontalScrollBarVisibility="Disabled"for the listbox, but the only result is that the scroll is hidden. The list items still have the same width (which is longer than the control's width).

我曾尝试添加ScrollViewer.HorizontalScrollBarVisibility="Disabled"列表框,但唯一的结果是滚动被隐藏。列表项仍然具有相同的宽度(比控件的宽度长)。

If I wasn't clear enough, just take a look at how twitter shows the tweets in a list, and that's what I want to do. Thanks.

如果我还不够清楚,请看一下推特如何在列表中显示推文,这就是我想要做的。谢谢。

UPDATE: This is basically what I want to achieve:

更新:这基本上是我想要实现的目标:

enter image description here

在此处输入图片说明

I was able to do this by explicitly setting the width of each column in the data template grid.

我能够通过明确设置数据模板网格中每列的宽度来做到这一点。

<Grid.ColumnDefinitions>
   <ColumnDefinition SharedSizeGroup="col1" Width="40" />
   <ColumnDefinition SharedSizeGroup="col2" Width="200" />
</Grid.ColumnDefinitions>

However, it's important the the list and it's items resize automatically when the window resizes.

但是,列表很重要,当窗口调整大小时,它的项目会自动调整大小。

采纳答案by Marius Bancila

I was able to achieve the desired result by replacing the Gridwith a DockPanel.

我能够通过将 替换GridDockPanel.

        <ListBox.ItemTemplate>
           <DataTemplate>
              <Border BorderThickness="0,1,0,0" BorderBrush="Black" Margin="0,0,0,5">
                 <DockPanel LastChildFill="True">
                    <Image DockPanel.Dock="Left" Source="{Binding Image}" Width="24" Height="24" Margin="0,5,0,5"
                           VerticalAlignment="Top"/>

                    <StackPanel DockPanel.Dock="Right" Orientation="Vertical"
                                Margin="5,2,0,0"
                                VerticalAlignment="Top">
                       <TextBlock Text="{Binding Title}" TextWrapping="Wrap" FontWeight="Bold" />
                       <TextBlock Text="{Binding Text}" TextWrapping="Wrap" FontSize="12" Margin="0,2,0,0" />
                    </StackPanel>
                 </DockPanel>
              </Border>
           </DataTemplate>
        </ListBox.ItemTemplate>

回答by paparazzo

On the TextBlock try:

在 TextBlock 上尝试:

Width="{Binding ElementName=alertsList, Path=ActualWidth}"> 

If you need to take some off then see my answer to this question

如果你需要脱掉一些,那么请看我对这个问题的回答

GridViewColumn Width Adjustment

GridView 列宽调整