我的ListView没有像我想要的那样运行,我似乎找不到任何关于这个主题的有用的东西。所以问题是,我有一个使用基于数据类型的DataTemplateSelector的ListView。因此,这一切都很好,并显示了正确的控制,但问题是,项目不能水平伸展!所有关于ListViewItems不伸展的解决方案都建议使用
<Style x:Key="ListViewItemContainerStyle" TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>并设置
ScrollViewer.HorizontalScrollBarVisibility="Disabled"在ListView中。
当没有使用DataTemplateSelector时,这确实可以很好地工作,但与DataTemplateSelector结合使用时,它就不会这样做,并且列表中的控件不会获得它们的父级大小。DataTemplateSelector是按规定的,工作正常,所以这里省略它。我尝试了Width=“自动”,HorizontalAlignment和HorizontalContentAlignment的所有组合,但我不能让它工作。我可以想象在这个DataTemplateSelector中传递HorizontalContentAlignment或Width可能会有所帮助,但是我找不到这样做的方法。
下面是最小的XAML:
<UserControl.Resources>
<Style x:Key="ListViewItemContainerStyle" TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
<DataTemplate x:Key="DrawDataTemplate" DataType="viewModels:DrawSectionViewModel">
<infrastructure:DrawSectionItem />
</DataTemplate>
<DataTemplate x:Key="FeedDataTemplate" DataType="viewModels:FeedSectionViewModel">
<infrastructure:FeedSectionItem />
</DataTemplate>
<infrastructure:SectionTemplateSelector x:Key="SectionTemplateSelector"
FeedSection="{StaticResource FeedDataTemplate}"
DrawSection="{StaticResource DrawDataTemplate}">
</infrastructure:SectionTemplateSelector>
</UserControl.Resources>
<ListView Grid.Column="0" Width="Auto" Margin="0,0,0,0"
ItemsSource="{Binding LeftSections }"
ItemTemplateSelector="{StaticResource SectionTemplateSelector}"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemContainerStyle="{StaticResource ListViewItemContainerStyle}" />任何帮助都将不胜感激
谢谢
添加了item控件。绘图部分的结构基本相同。
<UserControl x:Class="Infrastructure.FeedSectionItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="45" Width="188" FontFamily="Segoe UI Light" Background="Black"
>
<Grid Margin="0" >
<StackPanel Orientation="Vertical" Margin="10,0,0,0" HorizontalAlignment="Left" Width="Auto">
<TextBlock Text="Feed" Foreground="White" FontWeight="Bold" FontSize="16" />
<TextBlock Text="" Foreground="White" FontWeight="Bold" />
</StackPanel>
</Grid>
</UserControl>发布于 2021-01-12 17:14:32
从UserControl中删除Width="188"。
在设计器中,您可以设置d:DesignWidth="188"
https://stackoverflow.com/questions/65671218
复制相似问题