Why the listbox dose not show data?

aluzi liu 486 Reputation points
2022-09-13T11:10:42.813+00:00

[.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.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2022-09-14T01:53:14.683+00:00

    Hi, aluziliu-4133. Welcome Microsoft Q&A.
    The { get; set; } syntax defines your CollectionModelFile as a property . Without this, CollectionModelFile is simply a field. For more information, you can refer to the Auto-Implemented Properties .
    You could modify the TestModel class as follows.

     public class TestModel  
        {  
            public ObservableCollection<ModelFile> CollectionModelFile { get; set; } = new ObservableCollection<ModelFile>();  
      
            public TestModel()  
            {  
              ...  
            }  
        }  
    

    The result:

    240788-image.png

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful