我制作了一个使用Listview在特定位置渲染矩形的应用程序(用于图像分析的ROI)。这些从可观察的集合中获取其坐标。在Windows 7上,一切都按预期工作,但是在Windows 10上运行相同(二进制)代码,则为每个连续项添加了偏移。
这是重现此行为的最小工作示例。
预期:所有矩形都应在Win7和Win10中的同一位置绘制,从而形成一个统一的正方形
现实:这在Windows 7中正确呈现,但是在Windows 10中,ListViewItem中的Border的呈现高度不是0,而是Height = 4,因此,每个项目的矩形都呈现在不同的位置。显式地将Height设置为值<2将使矩形不可见。
App
using System.Collections.ObjectModel;
using System.Windows;
namespace GraphicListBoxTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static readonly DependencyProperty dataProperty =
DependencyProperty.Register("data", typeof(ObservableCollection<int>), typeof(MainWindow), new PropertyMetadata(null));
public ObservableCollection<int> data
{
get { return (ObservableCollection<int>)GetValue(dataProperty); }
set { SetValue(dataProperty, value); }
}
public MainWindow()
{
InitializeComponent();
data = new ObservableCollection<int>() { 1, 2, 3, 4 }; //Of course, the date is just fake.
}
}
}
Xaml:
<Window x:Class="GraphicListBoxTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GraphicListBoxTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Background="Black">
<Grid>
<ListView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent" ItemsSource="{Binding data, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="BorderThickness" Value="0"/>
<!-- Windows 7: 0, Windows 10:-->
<Setter Property="Height" Value="0"/>
<!-- Windows 7: 0, Windows 10: 4, Setting to 0 under Windows 10 completely hides the rectangles-->
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<!-- Windows 7 RenderingSize:Width,0 Windows 10: 4 -->
<DataTemplate x:Name="DT" >
<Canvas x:Name="Canvas">
<Rectangle Width="100" Height="100" Fill="#88FFFFFF">
<Canvas.Left>100</Canvas.Left>
<Canvas.Top>100</Canvas.Top>
</Rectangle>
</Canvas>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
如何获得与Windows 7相同的渲染?由于我在原始代码中使用了很多转换器,因此我不想仅通过运行号来解决此偏移量...
这是一个本地应用程序,因此不涉及服务器/浏览器。在两个系统上使用完全相同的二进制文件。
两个系统都使用.Net 4.7.2,未设置自定义设计。
[好,是我自己回答这个问题的时间:由于在不使所有内容消失的情况下无法将Height
设置为0,所以我尝试了其他一些可能性。
[将Height
设置为10并将Margin
设置为0,-10,0,0时,渲染正确。因此,渲染引擎假设您不能移至零以下?我认为那很奇怪,但至少对我有用。
这是我现在使用的样式:
<Style x:Key="FixWindows10Offset" TargetType="{x:Type ListViewItem}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Height" Value="10"/>
<Setter Property="Margin" Value="0,-10,0,0"/>
</Style>