简体   繁体   中英

What are the differences in ListViewItem between Windows 7 and Windows 10

I made an application which uses a Listview to render rectangles at certain positions (ROIs for image analyses). These get their coordinates out of an observable collection. On Windows 7, everything works as expected, but running the same (binary) code on Windows 10, an Offset is added for each consecutive item.

This is a minimal working example to reproduce this behaviour.

Expected: All rectangles should be rendered at the same position in Win7 and Win10, thus forming an uniform square

Reality: This is rendered correctly in Windows 7, but in Windows 10, the Border in the ListViewItem is not rendered with 0 Height, but with Height=4, thus the rectangle from every item is rendered at a different position. Setting the Height explicitly to a value < 2 renders the rectangles invisible.

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.
        }
    }
}

The 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>

How can I get the same rendering like in Windows 7? Since I use a lot of converters in my original code, I don't want to pass through the running number just to fix this offset...

This is a native app, so no servers / browsers are involved. The very same binaries are used on both systems.

Both systems use .Net 4.7.2, no custom Designs are set up.

OK, time to answer it for myself: Since it is not possible to set the Height to 0 without making everything disappear, I tried some other possibilities.

When setting the Height to 10 and the Margin to 0,-10,0,0 the rendering is correct. So the rendering engine assumes that you can't shift below Zero? I thinks thats strange, but at least works for me.

This is the style I use now:

    <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM