The.Anti.9
The.Anti.9

Reputation: 44738

WPF ListBox Item labels from bound collection are blank

I am having some trouble with my WPF styling. I have a ListBox which contains a bunch of blank (not supposed to be blank) labels. is defined in my XAML as follows:

            <ListBox Height="auto" Name="ProjectsList" Width="auto" DockPanel.Dock="Top" ItemsSource="{Binding Projects}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="3">
                            <Label Content="{Binding Path=Name}" Foreground="Black" Height="40" Padding="7" VerticalAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Name="ProjectName" />
                            <Label Content="{Binding Path=TaskCount}" Foreground="Gray" Width="25" Name="ProjectTaskCount" />
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Projects is an ObservableCollection which is filled with Project structs (Which I defined) which are created after a database query to grab the corresponding data. I can step through the code and see that the query comes back fine, and see the structs getting filled out and added to the collection. I also know that it's populating the ListBox Because I can select the blank item in it and it gets highlighted. But there is no text like there should be.

The Project attribute Name is a string and TaskCount is an int.

Here's the Project Definition

public struct Project
{
    public Int64 Id;
    public string Name;
    public string Description;
    public bool HasDueDate;
    public DateTime DueDate;
    public Int64 UserId;
    public int TaskCount;
}

The question is: why does the text not show up?

Upvotes: 0

Views: 1064

Answers (2)

David Bekham
David Bekham

Reputation: 2175

You should implement INotifyPropertyChanged to your business object. Which will directly notify the property changes to the UI using the PropertyChanged call back.. But i am not sure whether you are using this to bind the ListBox....

http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial

http://www.codeproject.com/Articles/81484/A-Practical-Quick-start-Tutorial-on-MVVM-in-WPF

http://wpftutorial.net/MVVM.html

Upvotes: 1

blindmeis
blindmeis

Reputation: 22445

you can just bind to public Properties. i just see fields on your struct. and like the others said: implement INotifyPropertyChanged and raise it properly

Upvotes: 3

Related Questions