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

时间:2009-08-05 09:33:55

标签: wpf xaml itemscontrol

当我在 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; }
    }

}

答案:

这是修复,谢谢肯特:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel Background="#ccc" SnapsToDevicePixels="True">
            <TextBlock Text="{Binding LastName}"/>
        </StackPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>

2 个答案:

答案 0 :(得分:9)

可能是WPF推断出一定量的透明度,因为这些项目不属于精确的像素边界。尝试使用项目容器上的SnapsToDevicePixels属性,看看是否有帮助。

答案 1 :(得分:0)

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

                 <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Background="DarkGreen" Margin="0,0,-0.5,-0.5">
                            <TextBlock Text="{Binding LastName}"/>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>