2017-09-05 3 views
0

J'ai créé un ListBox personnalisé avec chaque élément séparé par un séparateur. Je voyais un problème étrange. L'épaisseur du séparateur n'était pas constante entre les éléments de la liste. Ensuite, j'ai utilisé l'attribut UseLayoutRounding après la suggestion de stackoverflow. Mais maintenant, je vois un problème différent. Les séparateurs ne sont pas affichés sur certaines machines. Je veux dire, les séparateurs sont soit montrés ou non montrés dans certaines machines.Les séparateurs WPF Listbox ne sont pas affichés sur certaines machines

Voici le code source de la liste personnalisée.

<Window x:Class="CustListBox.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:CustListBox" 
      Title="MainWindow" Height="350" Width="525"> 
     <Window.Resources> 
      <local:Manager x:Key="manager"/> 
      <Style x:Key="LstStyle" TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ListBoxItem"> 
          <StackPanel> 
           <ContentPresenter/> 
           <Separator Foreground="Gray"/> 
          </StackPanel> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Window.Resources> 
     <Grid> 
      <ListBox Name="CustListBox" UseLayoutRounding="True" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source={StaticResource manager}, Path=UserList}" ItemContainerStyle="{Binding Source={StaticResource LstStyle}}" Margin="26,17,271,27"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
          <StackPanel>        
           <TextBlock Text="{Binding Path=FirstName}"/> 
           <TextBlock Text="{Binding Path=SecondName}"/>        
          </StackPanel>    
        </DataTemplate> 
       </ListBox.ItemTemplate>    
      </ListBox> 
      <ListBox Height="278" HorizontalAlignment="Left" Margin="264,16,0,0" Name="listBox1" VerticalAlignment="Top" Width="218" ItemsSource="{Binding Source={StaticResource manager}, Path=Names}" ItemContainerStyle="{Binding Source={StaticResource LstStyle}}"/> 
     </Grid> 
    </Window> 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace CustListBox 
{ 
    class Manager : PropertyChangeNotifier 
    { 
     public List<UserDetails> UserList { get; set; } 
     private int myVar; 

     public int MyProperty 
     { 
      get { return myVar; } 
      set { myVar = value; } 
     } 

     public List<string> Names { get; set; } 

     public Manager() 
     { 
      UserList = new List<UserDetails>(5); 
      Names = new List<string>(); 

      UserDetails usrDtls = new UserDetails(); 
      usrDtls.FirstName = "First Name"; 
      usrDtls.SecondName = "Second Name"; 

      UserList.Add(usrDtls); 
      UserList.Add(usrDtls); 
      UserList.Add(usrDtls); 
      UserList.Add(usrDtls); 
      UserList.Add(usrDtls); 
      UserList.Add(usrDtls); 
      UserList.Add(usrDtls); 
      UserList.Add(usrDtls); 

      Names.Add("Test Name"); 
      Names.Add("Test Name"); 
      Names.Add("Test Name"); 
      Names.Add("Test Name"); 
      Names.Add("Test Name"); 
      Names.Add("Test Name"); 
      Names.Add("Test Name"); 
      Names.Add("Test Name"); 
      Names.Add("Test Name"); 
      Names.Add("Test Name"); 
     } 
    } 
} 
namespace CustListBox 
{ 
    class UserDetails 
    { 
     public string FirstName { get; set; } 
     public string SecondName { get; set; } 
    } 
} 

Toute aide sera appréciée.

Répondre

0

A Separator est rien, mais un Border alors essayez de le remplacer par ceci:

<Style x:Key="LstStyle" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <StackPanel> 
        <ContentPresenter/> 
        <Border Background="Gray" 
          Height="1" SnapsToDevicePixels="true" 
          Margin="0,2,0,2"/> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

Notez que la propriété est définie sur SnapsToDevicePixelstrue.

+0

Merci pour la réponse. Je vais vérifier selon vos suggestions. – user3863360