【问题标题】:What are the differences in ListViewItem between Windows 7 and Windows 10Windows 7 和 Windows 10 的 ListViewItem 有什么区别
【发布时间】:2020-05-16 03:09:01
【问题描述】:

我制作了一个应用程序,它使用 Listview 在某些位置渲染矩形(用于图像分析的 ROI)。这些从可观察的集合中获取坐标。在 Windows 7 上,一切都按预期运行,但在 Windows 10 上运行相同的(二进制)代码,会为每个连续项目添加一个偏移量。

这是重现此行为的最小工作示例。

预期:所有矩形在Win7和Win10中都应该渲染在相同的位置,从而形成一个统一的正方形

现实:这在 Windows 7 中正确呈现,但在 Windows 10 中,ListViewItem 中的边框不是以 0 高度呈现,而是以高度 = 4 呈现,因此每个项目的矩形都呈现在不同的位置。将高度显式设置为

应用程序

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,没有设置自定义设计。

【问题讨论】:

  • 这不能依赖于操作系统。您可能更改了浏览器、Web-Server 或 .NET-Framework-Version ?也许它与 Metro-Style 有关,它已经在 Windows 8 中引入并改变了一些设计。
  • 我在两台计算机上使用完全相同的二进制代码,它是一个应用程序,因此不涉及浏览器或服务器。
  • 为什么这个问题被否决了?我该怎么做才能让它变得更好?
  • 通常它过于宽泛或不具体,或者无法重现。但你是对的,投反对票的人应该发表评论。我还没有真正的解决方案,但更好的问题是:&lt;Setter Property="Height" Value="4"/&gt; 这个设置来自哪里,谁生成了这个“4”。它一定来自某个地方——你只是猜测它与操作系统有关。在更改整个系统时,您无法知道还有哪些其他更改。

标签: c# xaml windows-7 windows-10


【解决方案1】:

好的,是时候自己回答了:由于无法将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>

【讨论】:

    猜你喜欢
    • 2010-10-27
    • 2018-06-22
    • 2015-12-26
    • 2016-11-22
    • 2013-12-10
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多