首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用DataTemplateSelector时,ListViewItem内容不会拉伸

使用DataTemplateSelector时,ListViewItem内容不会拉伸
EN

Stack Overflow用户
提问于 2021-01-12 00:55:24
回答 1查看 27关注 0票数 0

我的ListView没有像我想要的那样运行,我似乎找不到任何关于这个主题的有用的东西。所以问题是,我有一个使用基于数据类型的DataTemplateSelector的ListView。因此,这一切都很好,并显示了正确的控制,但问题是,项目不能水平伸展!所有关于ListViewItems不伸展的解决方案都建议使用

代码语言:javascript
复制
<Style x:Key="ListViewItemContainerStyle" TargetType="ListViewItem">
     <Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>

并设置

代码语言:javascript
复制
   ScrollViewer.HorizontalScrollBarVisibility="Disabled"

在ListView中。

当没有使用DataTemplateSelector时,这确实可以很好地工作,但与DataTemplateSelector结合使用时,它就不会这样做,并且列表中的控件不会获得它们的父级大小。DataTemplateSelector是按规定的,工作正常,所以这里省略它。我尝试了Width=“自动”,HorizontalAlignment和HorizontalContentAlignment的所有组合,但我不能让它工作。我可以想象在这个DataTemplateSelector中传递HorizontalContentAlignment或Width可能会有所帮助,但是我找不到这样做的方法。

下面是最小的XAML:

代码语言:javascript
复制
 <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控件。绘图部分的结构基本相同。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2021-01-12 17:14:32

UserControl中删除Width="188"

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65671218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档