[.net6 wpf]
I have a xaml page:
<Window x:Class="miniAlertNEO.TestForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:miniAlertNEO"
mc:Ignorable="d"
Title="TestForm" Height="350" Width="400">
<Window.DataContext>
<local:TestModel/>
</Window.DataContext>
<Window.Resources>
<CollectionViewSource x:Key="ListBoxItems" Source="{Binding CollectionModelFile}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Category" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<ListBox Name="lbMain" ItemsSource="{Binding Source={StaticResource ListBoxItems}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="30" Orientation="Horizontal">
<Label Width="20"/>
<CheckBox IsChecked="{Binding IsChecked}" VerticalAlignment="Center"/>
<TextBlock Text="{Binding FilterCond}" Margin="5 0 10 0" VerticalAlignment="Center"/>
<TextBox Text="{Binding FilterValue}" Width="60" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" VerticalAlignment="Center" TextAlignment="Center" Background="YellowGreen"/>
<ItemsPresenter Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
</Grid>
</Window>
The listbox I have setup it's DataContext, and source code is:
namespace miniAlertNEO
{
public class ModelFile
{
public string? Category { get; set; }
public int Num { get; set; }
public bool IsChecked { get; set; }
public string? FilterCond { get; set; }
public double FilterValue { get; set; }
}
class TestModel
{
public ObservableCollection<ModelFile> CollectionModelFile = new ObservableCollection<ModelFile>();
public TestModel()
{
CollectionModelFile.Add(new ModelFile() { Category = "FirstClass", Num = 0, IsChecked = true, FilterCond = "Width", FilterValue = 512 });
CollectionModelFile.Add(new ModelFile() { Category = "FirstClass", Num = 0, IsChecked = true, FilterCond = "Height", FilterValue = 7 });
CollectionModelFile.Add(new ModelFile() { Category = "FirstClass", Num = 0, IsChecked = true, FilterCond = "Length", FilterValue = 3 });
CollectionModelFile.Add(new ModelFile() { Category = "SecondClass", Num = 1, IsChecked = true, FilterCond = "Width", FilterValue = 50 });
CollectionModelFile.Add(new ModelFile() { Category = "SecondClass", Num = 2, IsChecked = true, FilterCond = "height", FilterValue = 50 });
}
}
public partial class TestForm : Window
{
public TestForm()
{
InitializeComponent();
}
}
}
When I ran this sample, I did not see listbox showing data, why?
I debug to TestModel, I am sure that CollectionModelFile is initialized with 5 elements.