ItemsControlの項目に背景色を設定し、余白を0に設定すると、WPFはItemsControlラッパー配管がわずかな量のスペースを占めているかのように項目間にヘアラインを残します。 スヌープでビジュアルツリーをチェックし、すべてのマージンを0,0,0,0に設定しました。ItemsControlアイテム間に薄いヘアライン(1ピクセル?)があるのはなぜですか?
これらの行の原因と回避方法を教えてください。
alt text http://i32.tinypic.com/9tzh1d.png
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>
ありがとうございました。私は上記のSnapsToDevicePixelsを使用してコードを投稿しました。 –
ありがとう、これは難しいことがあります。隠し余白/パディングを探しているテンプレートを掘った回数を数えることはできません。これを最初にチェックすることを忘れないと良いでしょう。 – kbo4sho88