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

时间:2020-05-24 20:47:48

标签: wpf listview user-controls dpi

我有一个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演示者似乎确实存在问题,这可能是一个错误吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

我希望它也能帮助其他人!

相关问题