一千萬個為什麽

搜索

奇怪的WPF ListBox行為

我正在嘗試將項目列表綁定到WPF中的 ListBox 。這些項目按一個值分組,每個組都包含在 Expander 中。當我不使用任何自定義樣式時,一切正常。但是,當我使用自定義樣式(與未分組的項目和獨立控件一起正常工作)時,綁定不會顯示任何項目。下面是我正在執行的代碼。任何想法為什麽項目不會出現在 Expander

Test.xaml:



    
        
              
            
                
            
        
     


        
    
        
            
        
    

Test.xaml.cs:

public partial class Test : Window
{
    private List _ContactItems;
    public List ContactItems
    {
        get { return _ContactItems; }
        set { _ContactItems = value; }
    }

    public Test()
    {
        InitializeComponent();

        ContactItems = new List();
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "ABC";
        ContactItems.Last().Name = "Contact 1";
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "ABC";
        ContactItems.Last().Name = "Contact 2";
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "ABC";
        ContactItems.Last().Name = "Contact 3";
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "ABC";
        ContactItems.Last().Name = "Contact 10";
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "ABC";
        ContactItems.Last().Name = "Contact 11";
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "ABC";
        ContactItems.Last().Name = "Contact 12";

        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "RST";
        ContactItems.Last().Name = "Contact 7";
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "RST";
        ContactItems.Last().Name = "Contact 8";
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "RST";
        ContactItems.Last().Name = "Contact 9";

        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "XYZ";
        ContactItems.Last().Name = "Contact 4";
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "XYZ";
        ContactItems.Last().Name = "Contact 5";
        ContactItems.Add(new Contact());
        ContactItems.Last().CompanyName = "XYZ";
        ContactItems.Last().Name = "Contact 6";

        ICollectionView view = CollectionViewSource.GetDefaultView(ContactItems);
        view.GroupDescriptions.Add(new PropertyGroupDescription("CompanyName"));
        view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
        TestList.ItemsSource = view;
    }
}

public class Contact
{
    public string CompanyName { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

TestStyles.xaml:




    
        
    
    
        <trigger Property="ToggleButton.IsMouseOver" Value="True">
            
        
        <trigger Property="IsPressed" Value="True">
            
        
        <trigger Property="IsChecked" Value="True">
            
        
        <trigger Property="IsEnabled" Value="False">
            
            
            
            
        
    




最佳答案

ListBox 控件模板(在TestStyles.xaml中)中使用 ItemsPresenter 而不是 StackPanel

轉載註明原文: 奇怪的WPF ListBox行為