What do I have to change in my xaml/code to make the binding to the properties => SchoolclassName and LessonName
to work on both TextBlock
s? I get no Binding errors but I do not see anything displayed:
<Grid Margin="20" Height="300" Background="AliceBlue">
<ListView ItemsSource="{Binding Timetable}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Period"
DisplayMemberBinding="{Binding LessonPeriod}"/>
<GridViewColumn Header="Mo开发者_如何学Pythonnday">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Monday}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Width="150" Orientation="Horizontal">
<TextBlock Text="{Binding LessonName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding SchoolclassName}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
public partial class Window1 : Window
{
List<TimetableEntry> _timetable = new List<TimetableEntry>();
public List<TimetableEntry> Timetable
{
get { return _timetable; }
set { _timetable = value; }
public Window1()
{
InitializeComponent();
_timetable.Add(new TimetableEntry()
{
LessonPeriod = "Period 1",
Monday = new TimetableDay()
{
LessonName = "Maths" ,
SchoolclassName = "1c",
},
}
);
this.DataContext = this;
}
public class TimetableEntry
{
public string LessonPeriod { get; set; }
public TimetableDay Monday { get; set; }
public TimetableDay Tuesday { get; set; }
public TimetableDay Wednesday { get; set; }
public TimetableDay Thursday { get; set; }
public TimetableDay Friday { get; set; }
public TimetableDay Saturday { get; set; }
public TimetableDay Sunday { get; set; }
}
public class TimetableDay
{
public string LessonName { get; set; }
public string SchoolclassName { get; set; }
}
public class TimetableLesson
{
public string LessonName { get; set; }
public string SchoolclassName { get; set; }
public DateTime LessonTime { get; set; }
}
}
You are setting the inner ListView's ItemsSource to be the Monday property. But Monday is a TimetableDay, not a collection, so there is no "list" for the inner list view to enumerate.
You probably just want to get rid of the inner ListView, and have the DataTemplate bind directly off the TimetableEntry, using subproperties of the Monday property:
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Width="150" Orientation="Horizontal">
<TextBlock Text="{Binding Monday.LessonName}" /> <!-- note multipart path -->
<TextBlock Text=" " />
<TextBlock Text="{Binding Monday.SchoolclassName}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
精彩评论