2017-12-27 30 views

답변

0

ListBox로 확장하려면 ListBox의 ItemTemplate을 사용해보십시오. ScreenShot XAML :

<Window x:Class="WpfApp1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApp1" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.DataContext> 
    <local:MainWindowVM/> 
</Window.DataContext> 
<Grid> 
    <ListView ItemsSource="{Binding ItemsList}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <Expander Header="{Binding Name}" > 
        <ListView ItemsSource="{Binding Items}"/> 
       </Expander> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</Grid> 

뷰 모델 : 예를 들어

하는 즉시 WPF 프레임 워크에있는 한
public class MainWindowVM 
{ 
    public MainWindowVM() 
    { 
     ItemsList = new List<Group>(); 
     var items = new List<Item>(); 
     items.Add(new Item("Item1")); 
     items.Add(new Item("Item2")); 
     items.Add(new Item("Item3")); 
     //items.Add("Item4"); 
     //items.Add("Item5"); 
     ItemsList.Add(new Group() 
     { 
      Name = "List1", 
      Items = items 
     }); 

     items.Add(new Item("Item4")); 
     ItemsList.Add(new Group() 
     { 
      Name = "List2", 
      Items = items 
     }); 
     items.Add(new Item("Item5")); 
     ItemsList.Add(new Group() 
     { 
      Name = "List3", 
      Items = items 
     }); 
    } 

    public List<Group> ItemsList { get; set; } 
} 

public class Group 
{ 
    public string Name { get; set; } 
    public List<Item> Items { get; set; } 
    public override string ToString() 
    { 
     return Name; 
    } 
} 
public class Item 
{ 
    public Item(string name) 
    { 
     Name = name; 
    } 
    public string Name { get; set; } 
    public override string ToString() 
    { 
     return Name; 
    } 
} 
+0

런타임 중에리스트를 어떻게 변경합니까? – Jecke

+0

인터페이스를 구현해야합니다. INotifyPropertyChanged –

0

, 당신은 ListBox.GroupStyle뿐만 아니라 정기적 인 스타일을 정의 할 수 있습니다 내부 항목 용. (축소 또는 확대) http://www.c-sharpcorner.com/uploadfile/dpatra/grouping-and-sorting-in-listbox-in-wpf

이 그룹을 확장/축소하려면이 현재 상태를 유지하는 부울 속성을 추가 할 수 걸릴 모든 크레딧 여기

ICollectionView view = CollectionViewSource.GetDefaultView(*your-collection-here*); 
view.GroupDescriptions.Add(new PropertyGroupDescription(*your-grouping-field-here*)); 
view.SortDescriptions.Add(new SortDescription((*your-grouping-field-here*, ListSortDirection.Ascending)); 
your-listbox-here.ItemsSource = view; 

: 다음과 같은 일을 CollectionViewSource하기 위해 목록 상자를 바인딩 이것은 당신의 스타일 내부에서 고려해야합니다 (말하자면, 확장 된 아이템에만 보이게하고, "확장/붕괴"쉐브론 아이콘 등을 변경하십시오).

+0

bool 속성 대신 Андрій Петрук의 대답과 비슷한 "Expanders"를 사용할 수 있습니까? 그렇다면 어떻게? – Jecke

+0

@ Jecke, "그룹 스타일"버튼을 추가해야합니다. 이 버튼의 목적은 뷰 모델의 bool 속성을 토글하는 것입니다. 그룹 스타일은 버튼 그림 ("아래쪽 화살표"에서 "위쪽 화살표"까지)을 자동 새로 고침하고 항목 스타일은 전체에서 가시성을 바꿀 수 있습니다 (Visibility = {Binding Path = Expanded, Converter = {StaticResource BoolToVisibilityConverter}})) –