我想創建一個分成若干組,使得從任何一組物品都可以隱藏列表。
現在,每個組的項目在一個單獨的收集和所有集合(共5個)的單個對象舉行舉行。但是如果它能使代碼更簡單,我可以修改它們的存儲方式。
我想創建一個分成若干組,使得從任何一組物品都可以隱藏列表。
現在,每個組的項目在一個單獨的收集和所有集合(共5個)的單個對象舉行舉行。但是如果它能使代碼更簡單,我可以修改它們的存儲方式。
試試這個,列表框的易變化的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>
視圖模型:
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;
}
}
只要你與WPF框架,你可以定義ListBox.GroupStyle以及定期的風格內部物品。然後結合你的列表框CollectionViewSource類似的東西:
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;
所有學分去這裏:http://www.c-sharpcorner.com/uploadfile/dpatra/grouping-and-sorting-in-listbox-in-wpf
要展開/摺疊組,您可以添加,保持當前狀態的布爾屬性(摺疊或展開),並採取這爲您的樣式(讓的說,使之可見爲展開的項目只,變「展開/摺疊」 V形圖標等)
是否可以使用「擴展器」,類似АндрійПетрук的答案,而不是布爾財產?如果是這樣 - 如何? – Jecke
@Jecke,你必須添加一個按鈕「組風格」。該按鈕的用途是切換視圖模型中的布爾屬性。然後,組風格可以自動刷新其按鈕的圖片(來自「向下箭頭」到「向上箭頭」)和項目風格可以在整個(像能見度更改其可見= {綁定路徑=擴大,轉換器= {StaticResource的BoolToVisibilityConverter}} ) –
我怎麼會在運行時更改列表裏面的帳戶? – Jecke
您需要實現接口INotifyPropertyChanged的 –