question

aluziliu-4133 avatar image
0 Votes"
aluziliu-4133 asked aluziliu-4133 commented

Why the listbox dose not show data?

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


dotnet-wpf-xamldotnet-standard
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered aluziliu-4133 commented

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks so much!

0 Votes 0 ·