DudeWhoWantsToLearn
DudeWhoWantsToLearn

Reputation: 771

TextBlock not showing up in ListView

This is my ListView:

<ListView ItemsSource="{Binding ModuleList}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <!--<Image Source="{Binding ModuleImage}" Stretch="UniformToFill"/>-->
                <TextBlock Text="{Binding ModuleName}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

This is the code in WPF (MVVM):

public ItemListViewVM() : base()
{
    ModuleList = new List<Module>();

    modulLoader.LoadAllModules();
    tempModulList = modulLoader.GetAllModules();

    foreach (var module in tempModulList)
    {
        ModuleImage = module.ModuleImage;
        ModuleName = module.Name;
        ModuleList.Add(module);
    }
}

Long story short: The List tempModulList contains Objects of type Module, which has an ImageSource Image and a string Name. Then the ModuleList gets one item after another. When I uncomment the Image in xaml, you can see it. But the TextBlock won't show up no matter what. I checked the string module.Namefor every item, it is not empty.

EDIT: Add Module Class The Module Class just contains Name and Image:

public class Module
{
    public ImageSource ModuleImage { get; set; }
    public string Name { get; set; }
}

The Object gets created by deserializing a Json

Upvotes: 0

Views: 196

Answers (1)

Clemens
Clemens

Reputation: 128061

The Bindings in the ItemTemplate use the properties of the data item class as their source properties. Hence you should write

Text="{Binding Name}"

instead of

Text="{Binding ModuleName}"

Unless you set the View property of a ListView (to e.g. a GridView) you could also better use a ListBox, which is the base class of ListView, and simpler:

<ListBox ItemsSource="{Binding ModuleList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding ModuleImage}"/>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Initializing the ModuleList property in the view model constructor would be as simple as this:

public ItemListViewVM()
{
    modulLoader.LoadAllModules();
    ModuleList = modulLoader.GetAllModules();
}

Upvotes: 2

Related Questions