WPF:包含 ListView 的垂直扩展器无法正确扩展

发布于 2024-10-10 15:49:31 字数 3632 浏览 2 评论 0原文

我有一个包含列表视图的扩展器。扩展器最初被禁用,并在应用程序启动后的某个时间启用。

如果在 InitializeComponent() 之前我用至少一项填充 ListView 的 ItemsSource (一些可观察的集合,当然是在 InitializeComponent 之前创建的),那么一切都会很好。扩展器正确扩展,项目按其应有的方式显示。唯一的问题是需要更多空间渲染的新项目会被裁剪 - 我认为这意味着 ListView 没有正确刷新?

但是,如果我在 InitializeComponent() 之后随时将项目添加到集合中,则扩展器只会扩展一点点(仅几个像素)。当我强制 ListView 的宽度为某个大值时,扩展器确实会扩展,但是插入列表中的项目仅使用背景颜色呈现,没有内容...

我认为这与 ListView 初始化、刷新有关或者你有什么。代码如下:

XAML:

<DockPanel Name="mainPanel">
    <Expander DockPanel.Dock="Right" ExpandDirection="Right" IsExpanded="False" IsEnabled="{Binding ExpanderEnabled}">
        <Expander.Style>
            <Style TargetType="Expander">
                <Setter Property="Background" Value="#FFFF3333" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ExpanderEnabled}" Value="True">
                        <Setter Property="Background" Value="LightGreen" />
                    </DataTrigger>
                </Style.Triggers>
             </Style>
         </Expander.Style>
         <Expander.Header>
             <Grid Width="30" VerticalAlignment="Top" HorizontalAlignment="Left">
                 <Grid Width="300" Height="300" VerticalAlignment="Top" HorizontalAlignment="Left">
                      <TextBlock Text="Expander Name" FontSize="12" FontWeight="Bold" Margin="22,170,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" RenderTransformOrigin="0,1">
            <TextBlock.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="-90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </TextBlock.RenderTransform>
                            </TextBlock>
                        </Grid>
                    </Grid>
         </Expander.Header>
         <ListView ItemsSource="{Binding Items}" BorderBrush="LightGreen" BorderThickness="3">
             <ListView.Style>
                 <Style TargetType="ListView">
                     <Style.Resources>
                         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black"/>
                     </Style.Resources>
                 </Style>
              </ListView.Style>
              <ListView.View>
                  <GridView>
                      <GridView.ColumnHeaderContainerStyle>
                          <Style TargetType="{x:Type GridViewColumnHeader}">
                              <Setter Property="Visibility" Value="Collapsed"/>
                          </Style>
                      </GridView.ColumnHeaderContainerStyle>
                      <GridViewColumn DisplayMemberBinding="{Binding Path=A, Converter={StaticResource aConverter}}" Width="Auto" />
                      <GridViewColumn DisplayMemberBinding="{Binding Path=B, Converter={StaticResource bConverter}}" Width="Auto" />
                      <GridViewColumn DisplayMemberBinding="{Binding Path=C, Converter={StaticResource cConverter}}" Width="Auto" />
                      <GridViewColumn DisplayMemberBinding="{Binding Path=D, StringFormat='\{0\} ds'}" Width="Auto" />
                  </GridView>
              </ListView.View>
           </ListView>
      </Expander>
      ...

I have an expander containing a list view. The expander is initially disabled, and enabled sometime after the application is started.

Everything works great if before InitializeComponent() I populate the ListView's ItemsSource (some observable collection, created of course before InitializeComponent) with at least one item. The expander expands correctly, items show as they should. The only problem then is that new items that require more space to be rendered appear cropped - I assume this means the ListView isn't refreshing properly?

However, if I add items to the collection anytime after InitializeComponent(), the expander only expands a little bit (just a few pixels). When I force the ListView's Width to be some large value, the expander does expand, but items inserted into the list are rendered just with the background color, with no content...

I assume this has something to do with the ListView initialization, refresh or what have you. Code follows:

XAML:

<DockPanel Name="mainPanel">
    <Expander DockPanel.Dock="Right" ExpandDirection="Right" IsExpanded="False" IsEnabled="{Binding ExpanderEnabled}">
        <Expander.Style>
            <Style TargetType="Expander">
                <Setter Property="Background" Value="#FFFF3333" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ExpanderEnabled}" Value="True">
                        <Setter Property="Background" Value="LightGreen" />
                    </DataTrigger>
                </Style.Triggers>
             </Style>
         </Expander.Style>
         <Expander.Header>
             <Grid Width="30" VerticalAlignment="Top" HorizontalAlignment="Left">
                 <Grid Width="300" Height="300" VerticalAlignment="Top" HorizontalAlignment="Left">
                      <TextBlock Text="Expander Name" FontSize="12" FontWeight="Bold" Margin="22,170,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" RenderTransformOrigin="0,1">
            <TextBlock.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="-90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </TextBlock.RenderTransform>
                            </TextBlock>
                        </Grid>
                    </Grid>
         </Expander.Header>
         <ListView ItemsSource="{Binding Items}" BorderBrush="LightGreen" BorderThickness="3">
             <ListView.Style>
                 <Style TargetType="ListView">
                     <Style.Resources>
                         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black"/>
                     </Style.Resources>
                 </Style>
              </ListView.Style>
              <ListView.View>
                  <GridView>
                      <GridView.ColumnHeaderContainerStyle>
                          <Style TargetType="{x:Type GridViewColumnHeader}">
                              <Setter Property="Visibility" Value="Collapsed"/>
                          </Style>
                      </GridView.ColumnHeaderContainerStyle>
                      <GridViewColumn DisplayMemberBinding="{Binding Path=A, Converter={StaticResource aConverter}}" Width="Auto" />
                      <GridViewColumn DisplayMemberBinding="{Binding Path=B, Converter={StaticResource bConverter}}" Width="Auto" />
                      <GridViewColumn DisplayMemberBinding="{Binding Path=C, Converter={StaticResource cConverter}}" Width="Auto" />
                      <GridViewColumn DisplayMemberBinding="{Binding Path=D, StringFormat='\{0\} ds'}" Width="Auto" />
                  </GridView>
              </ListView.View>
           </ListView>
      </Expander>
      ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文