为什么ItemsControl项目之间有细线(1像素?)?

问题描述 投票:2回答:2

当我在ItemsControl中的项目上有背景颜色,并将边距设置为0时,WPF会在项目之间留下细线,好像ItemsControl包装管道占用了一小段空间。我用Snoop检查了可视化树,所有边距都设置为0,0,0,0。

导致这些线路的原因是什么,我该如何避免它们?

alt text

XAML:

<DockPanel>

    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Background="Yellow" >
        <ItemsControl ItemsSource="{Binding CustomerList}">

            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Background="DarkGreen">
                        <TextBlock Text="{Binding LastName}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>

            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <DockPanel Margin="10"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

        </ItemsControl>
    </StackPanel>

</DockPanel>

代码隐藏:

using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace TestItemsControl2938
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        private ObservableCollection<Customer> _customerList = new ObservableCollection<Customer>();
        public ObservableCollection<Customer> CustomerList
        {
            get
            {
                return _customerList;
            }

            set
            {
                _customerList = value;
                OnPropertyChanged("CustomerList");
            }
        }

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            CustomerList.Add(new Customer { FirstName = "Jim", LastName = "Jones" });
            CustomerList.Add(new Customer { FirstName = "Joe", LastName = "Adams" });
            CustomerList.Add(new Customer { FirstName = "Jake", LastName = "Johnson" });
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }
    }

}

Answer:

这是修复,谢谢肯特:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel Background="#ccc" SnapsToDevicePixels="True">
            <TextBlock Text="{Binding LastName}"/>
        </StackPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>
wpf xaml itemscontrol
2个回答
9
投票

它可能是WPF推断一定量的透明度,因为项目不落在精确的像素边界上。尝试在项目容器上玩SnapsToDevicePixels属性,看看是否有帮助。


0
投票

我发现SnapToDevicePixes并不总是有效,而且在WINRT下它不可用。对我来说,最好的解决方案就是通过使用略微负的边距来填补项目之间的差距。

                 <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Background="DarkGreen" Margin="0,0,-0.5,-0.5">
                            <TextBlock Text="{Binding LastName}"/>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.