为什么ItemsControl的项目之间有薄的发际线(1个像素?)?
-
22-07-2019 - |
题
当我有上的项目的背景颜色中的的 ItemsControl的,然后将页边距设定为0,WPF离开项之间细线仿佛ItemsControl的包装器管道被占用的空间的微小量。我检查了视觉树的探听并所有边距设置为0,0,0,0。
什么是导致这些线,我怎么能避免呢?
<强> 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>
解决方案
这可能是WPF推断一定量的透明度,因为项目不落在确切像素边界。尝试用SnapsToDevicePixels
财产上的物品容器玩,看看有没有什么帮助。
其他提示
我发现SnapToDevicePixes
并不总是工作,而且,它不是在WinRT的使用。对于我的最佳解决方案是简单地通过使用一个边缘稍负填充项之间的间隙。
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Background="DarkGreen" Margin="0,0,-0.5,-0.5">
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
不隶属于 StackOverflow