WPF List Box
15-May-1414 Leave a comment
ItemTemplate – DataTemplate
<ListBox ItemsSource="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <StackPanel Orientation="Horizontal"> <StackPanel.Resources> <Style TargetType="TextBlock"> <Setter Property="Margin" Value="0, 0, 5, 0" /> </Style> </StackPanel.Resources> <TextBlock Text="{Binding Path=ID}" /> <TextBlock Text="{Binding Path=Task1}" /> <TextBlock Text="{Binding Path=PercentComplete}" /> </StackPanel> <TextBlock Text="Resource names here" Margin="50, 0, 0, 0" /> <!--<TextBlock Text="{Binding Path=ResourceName}" Margin="50, 0, 0, 0" />--> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Sorting
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" <CollectionViewSource x:Key="taskViewSource" d:DesignSource="{d:DesignInstance my:Task, CreateList=True}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="CompleteFlag" Direction="Descending" /> </CollectionViewSource.SortDescriptions> </CollectionViewSource>
Style DataTrigger
<ListBox ItemsSource="{Binding}"> <ListBox.Resources> <Style TargetType="{x:Type ListBoxItem}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=CompleteFlag}" Value="True"> <Setter Property="Background" Value="Yellow" /> </DataTrigger> </Style.Triggers> </Style> </ListBox.Resources> ... </ListBox>