ListView中相同高度的像素大小不正确:如何强制像素高度/大小

问题描述 投票:0回答:1

我有一个ListView,它承载C#WPF应用程序中用户控件的多个实例。

此用户控件显示一条始终具有相同大小“ 1”的水平线。

但是在屏幕上,应该具有相同高度的行显示为+/- 1像素的高度差!!

First line is thicker

First two lines are thinner!!!

问题:如何强制渲染以相同的PIXEL高度渲染线条?!

这里是用户控件中的行定义:

用户控件的行定义:

<Rectangle Grid.Row         = "1"
           Grid.Column      = "0"
           Grid.ColumnSpan  = "2"
           Fill             = "DodgerBlue"
           Height           = "1"
           Margin           = "0, 1, 0, 2" />

和ListView:

<ListView ItemsSource                   = "{Binding FoundBooks, Mode=OneWay}"
          SelectedItem                  = "{Binding SelectedBookPreview}"
          Grid.Row                      = "1" 
          Grid.Column                   = "0" 
          SelectionMode                 = "Single"
          HorizontalContentAlignment    = "Stretch">

    <ListView.ItemTemplate>
        <DataTemplate DataType="{x:Type vm:vmLessonBookPreview}">
            <ucont:ucLessonBookListViewItem DataContext         = "{Binding}"
                                            HorizontalAlignment = "Stretch" 
                                            Margin              = "-3, 0, -3, 0" />
        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>

更多注释;

  • 行高仅取决于控件中的显示位置,而不取决于索引号(因此,即使我滚动,第一行的大小也总是与第二行相同,例如)
  • 经过ListViewItemsControlComboBox测试,所有相同的问题
  • ScrollViewer中手动添加的控件的行为不像这样
  • 使用px大小(例如Height = "2px"不会更改任何内容!)

这似乎真的与ItemsControl演示者有关,可能是一个错误吗?

wpf listview user-controls dpi
1个回答
0
投票

确定,解决方案非常简单,尽管发现它比其他任何事情都幸运...

我只需要将UseLayoutRounding添加到Rectangle控件...:

<Rectangle Grid.Row             = "1"
           Grid.Column          = "0"
           Grid.ColumnSpan      = "2"
           Fill                 = "DodgerBlue"
           UseLayoutRounding    = "True" 
           Height               = "1"
           Margin               = "0, 1, 0, 2" />

我希望它也会对其他人有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.