ListView 中相同高度的像素大小不正确:如何强制像素 height/size
Incorrect pixel size for same height in ListView: how to enforce pixel height/size
我有一个 ListView
在 C# WPF 应用程序中承载用户控件的多个实例。
此用户控件显示始终具有相同大小“1”的水平线。
但是在屏幕上,应该具有相同高度的线显示为 +/-1 像素高度差!!!
问题:如何强制渲染将渲染具有相同 PIXEL 高度的线条?!
这里是用户控件中的Line定义:
用户控件的行定义:
<Rectangle Grid.Row = "1"
Grid.Column = "0"
Grid.ColumnSpan = "2"
Fill = "DodgerBlue"
Height = "1"
Margin = "0, 1, 0, 2" />
和列表视图:
<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>
更多笔记;
- 行高仅取决于控件中的显示位置,而不取决于索引号(因此第一行的大小始终与第二行的大小不同,例如,即使我滚动)
- 使用
ListView
、ItemsControl
和 ComboBox
进行了测试,所有问题都相同
- 在
ScrollViewer
中手动添加的控件不会像这样
- 使用 px 大小(例如
Height = "2px"
不会改变任何东西!)
看来真的是 ItemsControl 展示器的问题,会不会是一个错误?
好的,解决方案非常简单,虽然找到它比其他任何事情都更幸运...
我只需要将 UseLayoutRounding 添加到 Rectangle 控件中...:[=11=]
<Rectangle Grid.Row = "1"
Grid.Column = "0"
Grid.ColumnSpan = "2"
Fill = "DodgerBlue"
UseLayoutRounding = "True"
Height = "1"
Margin = "0, 1, 0, 2" />
希望对其他人也有帮助!
我有一个 ListView
在 C# WPF 应用程序中承载用户控件的多个实例。
此用户控件显示始终具有相同大小“1”的水平线。
但是在屏幕上,应该具有相同高度的线显示为 +/-1 像素高度差!!!
问题:如何强制渲染将渲染具有相同 PIXEL 高度的线条?!
这里是用户控件中的Line定义:
用户控件的行定义:
<Rectangle Grid.Row = "1"
Grid.Column = "0"
Grid.ColumnSpan = "2"
Fill = "DodgerBlue"
Height = "1"
Margin = "0, 1, 0, 2" />
和列表视图:
<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>
更多笔记;
- 行高仅取决于控件中的显示位置,而不取决于索引号(因此第一行的大小始终与第二行的大小不同,例如,即使我滚动)
- 使用
ListView
、ItemsControl
和ComboBox
进行了测试,所有问题都相同 - 在
ScrollViewer
中手动添加的控件不会像这样 - 使用 px 大小(例如
Height = "2px"
不会改变任何东西!)
看来真的是 ItemsControl 展示器的问题,会不会是一个错误?
好的,解决方案非常简单,虽然找到它比其他任何事情都更幸运...
我只需要将 UseLayoutRounding 添加到 Rectangle 控件中...:[=11=]
<Rectangle Grid.Row = "1"
Grid.Column = "0"
Grid.ColumnSpan = "2"
Fill = "DodgerBlue"
UseLayoutRounding = "True"
Height = "1"
Margin = "0, 1, 0, 2" />
希望对其他人也有帮助!