Wpf 项目宽度在控件宽度之后的列表框
我正在构建一个WPF用户控件,其中包含一个列表,其中的项目显示为图标和文本。但是,文本有不同的长度,我希望它们水平滚动。相反,我希望这些项目与用户控件具有相同的宽度,并且文本被包装显示(因此具有垂直滚动) 这是我的XAMLWpf 项目宽度在控件宽度之后的列表框,wpf,user-controls,listbox,grid,datatemplate,Wpf,User Controls,Listbox,Grid,Datatemplate,我正在构建一个WPF用户控件,其中包含一个列表,其中的项目显示为图标和文本。但是,文本有不同的长度,我希望它们水平滚动。相反,我希望这些项目与用户控件具有相同的宽度,并且文本被包装显示(因此具有垂直滚动) 这是我的XAML <UserControl x:Class="Demo.NotificationsWidget" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http
<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>
但是,当窗口调整大小时,列表及其项目会自动调整大小,这一点很重要。在文本块上,请尝试:
Width="{Binding ElementName=alertsList, Path=ActualWidth}">
如果你需要脱掉一些,那么看看我对这个问题的回答
我通过用
DockPanel
替换网格
,实现了预期的结果
<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>
<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>