2009-08-05 23 views
2

ItemsControlの項目に背景色を設定し、余白を0に設定すると、WPFはIt​​emsControlラッパー配管がわずかな量のスペースを占めているかのように項目間にヘアラインを残します。 スヌープでビジュアルツリーをチェックし、すべてのマージンを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> 

答えて

9

それは項目が正確なピクセル境界に該当しないため、WPFは、透明性の一定量を推測することがあります。アイテムコンテナのSnapsToDevicePixelsプロパティで試してみて、それが役立つかどうか確認してください。

+0

ありがとうございました。私は上記のSnapsToDevicePixelsを使用してコードを投稿しました。 –

+0

ありがとう、これは難しいことがあります。隠し余白/パディングを探しているテンプレートを掘った回数を数えることはできません。これを最初にチェックすることを忘れないと良いでしょう。 – kbo4sho88

0

私は、SnapToDevicePixesが必ずしも機能しないことが判明しました。さらに、WINRTでは利用できません。私の場合、最善の解決策は、わずかにマイナスのマージンを使ってアイテム間のギャップを埋めることです。

    <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Background="DarkGreen" Margin="0,0,-0.5,-0.5"> 
          <TextBlock Text="{Binding LastName}"/> 
         </StackPanel> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
関連する問題