views:

66

answers:

1

Hello,

what do I have to change in my xaml/code to make the binding to the properties => SchoolclassName and LessonName work on both TextBlocks? 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>
                            <GridViewColumn Header="Monday">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ListView ItemsSource="{Binding Monday}" >
                                        <ListView.ItemTemplate >
                                            <DataTemplate>
                                                <StackPanel Width="150" Orientation="Horizontal">
                                                    <TextBlock Text="{Binding LessonName}"></TextBlock>
                                                    <TextBlock Text=" " />
                                                    <TextBlock Text="{Binding SchoolclassName}"></TextBlock>
                                                </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; }
    }
}
+1  A: 

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>
itowlson
yes your are right , when I get rid of the inner ListView it works! BUT I need that ListView because I want to add another 6 ListView.ItemTemplates to add 6 columns one for each weekday:Monday,Tuesday,Wednesday...How could I handle that without introducing the List<> again?
msfanboy
ups sorry my failure I forgot to add the Tuesday and so on GridViewColumn , works now!
msfanboy
ah I am really too fast with my checks and tests ;Pthe problem is now, that in my DataGrid which I use now instead of a ListView , the lessons start on Tuesday where they stopped on Monday...see image to make it clear: [URL=http://img64.imageshack.us/i/classn.png/][IMG]http://img64.imageshack.us/img64/9691/classn.png[/IMG][/URL]The lessons on tuesday start directly after the lessons on monday.how can I make the lessons start on tuesday at the 1,2,3 hour?
msfanboy
as I can post only 600 chars I have put the new code on pastebin:http://pastebin.com/m3bcdc927
msfanboy
You need to restructure your classes to reflect the demands of the view. (You don't need to distort your model to do this; you can do it by adding a "view model" adapter layer.) In the view, you want one row per lesson period, with entries in that row for Mon, Tue, etc. So you need your ItemsSource to be a `List<Period>`, where each `Period` would have Monday, Tuesday, etc. properties. E.g. Period 1 would have Monday = Maths 12c, Tuesday = BWL 4a, etc. You can build this "view model" from your existing `List<TimetableEntry>` by merging entries with the same period.
itowlson
do you speak of MVVM?could you make a sample for this:"from your existing List<TimetableEntry> by merging entries with the same period"do not fully understand what you mean.
msfanboy
Yes, this would result in a simple MVVM architecture. Re merging, consider `class Period { string name; TimetableDay Monday; etc. }` and `ObservableCollection<Period> _periods`. Then go over your model (`_timetable`) and for each TimetableEntry find the appropriate Period in `periods`. Add the TE's Monday to the Period's Monday, the TE's Tuesday to the Period's Tuesday, etc. Basically you are trying to transform your current model into something row-oriented for your view. You're better placed to figure out the details, though, because I don't know your domain!
itowlson
but why row-oriented? why can I not make everything column oriented as a weekday is a column! I have no problem in changing my Model so the View is working fine for a weekday/each column.time for bed now ;-)
msfanboy
Your view model needs to be row-oriented because you are using a ListView, which is a row-oriented control. That is, ListView.ItemsSource represents the set of *rows* to be displayed, not the set of *cells* (which is what your `List<TimetableEntry>` appears to be). Alternatively of course you can use a different, cell-oriented control: for example you could use a ListBox with its ItemsPanel set to a Grid, and bind the Grid.X and Grid.Y properties in ItemContainerStyle to specify the cell for each lesson.
itowlson
"Your view model needs to be row-oriented":OK then I go with the DataGrid (because I want the lesson number/descriptions like 'lunch' in a separate row header) + MVVM as you said. Due to 600 chars only I have to split my idea...My idea how to solve it then, please correct where I am wrong. I want to learn from someone having12.3K posts ;P =>PART1:DOMAIN entities: TimeTableDay 1 : N TimeTableLessonTimeTableDay:- TimeTableDayID (PK)TimeTableLesson:- TimeTableLessonID (PK)- LessonNumber- LessonColor- SchoolclassName- SubjectName- TimeTableDayID (FK)PART2 following =>
msfanboy
PART2:ah I will try some MVVM next days and return here with a test scenario thats better instead of talking all day :) hope ya still around then :>
msfanboy
Hello itowlson,I havent forgotten you ;-) am just busy as hell with another question/real life...When I deal again with your last suggestion/idea to make the ViewModel care about the structure of my data I will open a new thread because its totall different question. Thanks a bunch again!
msfanboy