ItemsControlアイテムの間に細いヘアライン(1ピクセル?)があるのはなぜですか?
-
22-07-2019 - |
質問
ItemsControl のアイテムに背景色を設定し、マージンを0に設定すると、Itemsラッパーの配管がわずかなスペースを占有しているように、アイテム間にヘアラインが残ります。 Snoop でビジュアルツリーをチェックしました。すべてのマージンが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; }
}
}
回答:
修正プログラムです。Kentに感謝します。
<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