Damo
Damo

Reputation: 2070

Data Template not shown

A listbox data template doesn't show and I cannot figure out why.

If I don't use a DataTemplate and copy the contents into the control section itself, it's fine.

I don't do very much binding in XAML, I usually do it all in code. What did I do wrong?

XAML

   <UserControl x:Class="Cis.CustomControls.CisArrivalsPanel"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" Height="296" Width="876">
      <UserControl.Resources>
            <DataTemplate x:Key="DataTemplate">
                <ListBoxItem>
                    <StackPanel>
                        <TextBlock  Background="Blue" Text="{Binding Path=StationName}" />
                        <TextBlock  Background="Brown" Text="{Binding Path=ArrivalPlatform}" />
                    </StackPanel>
                </ListBoxItem>
            </DataTemplate>
        </UserControl.Resources>
        <Grid>
            <StackPanel Orientation="Horizontal">
                <ListBox Width="487" Margin="0,66,0,33" ItemTemplate="{StaticResource DataTemplate}">
                </ListBox>
            </StackPanel>
        </Grid>
    </UserControl>

CS

 public partial class CisArrivalsPanel : UserControl
    {
        public CisArrivalsPanel()
        {
            InitializeComponent();
            this.DataContext = new ArrivalRowItem();

        }
    }

Model

public class ArrivalRowItem : INotifyPropertyChanged
    {

        public ArrivalRowItem()
        {
            this.StationName = "Lincoln";
            this.ArrivalPlatform = "1";
        }


        private string _stationName;
        public string StationName
        {
            get
            {
                return _stationName;
            }
            set
            {
                _stationName = value;
                NotifyPropertyChanged("StationName");
            }
        }

        private string _arrivalPlatform;
        public string ArrivalPlatform
        {
            get
            {
                return _arrivalPlatform;
            }
            set
            {
                _arrivalPlatform = value;
                NotifyPropertyChanged("ArrivalPlatform");
            }
        }


        private DateTime _arrivalDateTime;
        public DateTime ArrivalDateTime
        {
            get
            {
                return _arrivalDateTime;
            }
            set
            {
                _arrivalDateTime = value;
                NotifyPropertyChanged("ArrivalDateTime");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

    }

Upvotes: 2

Views: 50

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61349

You have everything set up, but you don't actually have any data.

ListBox, like other ItemsControls acts against a collection of data, and generates an instance of the template for each item it finds.

Given that you haven't set ItemsSource or populated any collection I can see, you need to create a collection (probably an ObservableCollection) and set the ItemsSource to it via binding. Then add some items to it, and the ListBox will display them!

Upvotes: 2

Related Questions