【问题标题】:ListViewItem content is not stretching when using a DataTemplateSelector使用 DataTemplateSelector 时,ListViewItem 内容未拉伸
【发布时间】:2021-01-11 16:55:24
【问题描述】:

我的 ListView 没有按我的意愿行事,而且我似乎找不到关于这个主题的任何有用信息。 所以问题是,我有一个 ListView,它使用基于 Datatype 的 DataTemplateSelector。 所以这一切都很好,并显示了正确的控制,但问题是,这些项目没有水平伸展! 所有关于 ListViewItems 不拉伸的解决方案都建议使用

<Style x:Key="ListViewItemContainerStyle" TargetType="ListViewItem">
     <Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>

设置

   ScrollViewer.HorizontalScrollBarVisibility="Disabled"

在列表视图中。

当没有使用 DataTemplateSelector 时,这确实可以正常工作,但与该 DataTemplateSelector 结合使用时它不会,并且列表中的控件不会获得其父级大小。 DataTemplateSelector 是书本上的,工作正常,所以这里省略了。 我尝试了 Width="Auto"、Horizo​​ntalAlignment 和 Horizo​​ntalContentAlignment 的各种组合,但我无法使其工作。我可以想象在此 DataTemplateSelector 中传递 Width 或 Horizo​​ntalContentAlignment 可能会有所帮助,但我找不到这样做的方法。

这是最小的 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}" />

任何帮助将不胜感激

谢谢

添加了项目控件。 Draw 部分的结构几乎相同。

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

【问题讨论】:

  • 你能分享一下基础设施的 xaml:DrawSectionItem 和基础设施:FeedSectionItem 吗?
  • 添加到主要问题
  • 尝试从您的用户控件中删除 Width="188"。
  • 从 UserControl 中删除宽度确实有效!但是,我只会认为这是一种解决方法,因为现在我不能真正设计我的 UserControl 了,因为它会自动收缩,因为没有设置宽度。将 Grid 设置为宽度让我回到了最初的问题。每次设计 Control... 作品后,我都可以删除宽度,但对于理论上简单的问题,这似乎不是一个干净的解决方案。
  • 好的,那一秒我想到了一个好的解决方法,那就是在控件中添加一个 MinWidth。然后它有一个可用于设计的宽度,并且 ListView 中的实际宽度也按预期工作。

标签: wpf xaml listview stretch


【解决方案1】:

从您的UserControl 中删除Width="188"

在设计器中可以设置d:DesignWidth="188"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2013-12-13
    • 2010-11-23
    • 2018-08-02
    相关资源
    最近更新 更多