未显示数据模板

Data Template not shown

列表框数据模板未显示,我不明白为什么。

如果我不使用 DataTemplate 并将内容复制到控制部分本身,那很好。

我在 XAML 中做的绑定不多,我通常都是在代码中完成。我做错了什么?

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();

        }
    }

型号

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));
            }
        }

    }

您已设置好所有内容,但实际上您没有任何数据

ListBox,与其他 ItemsControls 一样,作用于数据集合,并为它找到的每个项目生成一个模板实例。

鉴于您尚未设置 ItemsSource 或填充我能看到的任何集合,您需要创建一个集合(可能是 ObservableCollection)并为其设置 ItemsSource通过绑定。然后在其中添加一些项目,ListBox就会显示出来!