Is it possible to bind a GridView to 2 DataTemplate or 2 DataSource? RRS feed

  • Question

  • Hi,

    I have a GridView with a DataTemplate having 11 Text Blocks.
    8 TextBlocks are bind to CVS data source.
    I want to know:
    1. If is possible to bind 3 TextBlocks(txtHome, txtEvent, txtNote) to other datasource.
    2. The GridView cell to have 2 DataTemplate.

     <CollectionViewSource x:Name="MonWeekJsViewSource" />

      <DataTemplate x:Key="ClassJTemplate">
        <StackPanel  x:Name="panelMon" Orientation="Vertical" HorizontalAlignment="Left" Height="87"
                                Background="{Binding SubjectColorJ}" Width="148">

             <TextBlock x:Name="txtSubjectNameJ" Text="{Binding SubjectNameJ}" Foreground="Black" Height="19"
                    TextWrapping="Wrap" VerticalAlignment="Top" FontSize="14" FontWeight="SemiBold"/>
         <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="17" Background="{Binding SubjectColorJ}" Width="150">

        <TextBlock  x:Name="txtStartJ" Text="{Binding StartJ}" Canvas.ZIndex="87" Foreground="Black" Height="17" Width="38"
              Margin="0,0,0,0" VerticalAlignment="Center" FontSize="14"/>

       <TextBlock  x:Name="txtStartAMPMJ" Text="{Binding StartAMPMJ}" Canvas.ZIndex="87"
               Foreground="Black" Height="17" Width="25" Margin="0,0,0,0" VerticalAlignment="Center"
       <TextBlock Text="-" Foreground="Black" Height="17" Width="8  VerticalAlignment="Top"
       <TextBlock  x:Name="txtEndJ"  Text="{Binding EndJ}" Canvas.ZIndex="87" Foreground="Black"  Width="40" Height="17"
           VerticalAlignment="Top" FontSize="14"/>
       <TextBlock  x:Name="txtEndAMPMJ"  Text="{Binding EndAMPMJ}" Canvas.ZIndex="87" Foreground="Black"  Width="25" Height="17" VerticalAlignment="Top" FontSize="14"/>

         <TextBlock  x:Name="txtRoomNameJ" Text="{Binding RoomNameJ}" Foreground="Black" Width="150" Height="17"
            TextWrapping="Wrap" VerticalAlignment="Top" FontSize="14"/>

           <TextBlock  x:Name="txtTeacherNameShortJ" Text="{Binding TeacherNameShortJ}"
                   Foreground="Black"  Width="150" Height="17"  VerticalAlignment="top" FontSize="14"/>

      <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="17"  Width="150">

            <TextBlock  x:Name="txtHome" Text="Home" Foreground="Red"  Width="50" Height="17"
                      VerticalAlignment="top" FontSize="14"/>
            <TextBlock  x:Name="txtEventJ" Text="Event" Foreground="Navy"  Width="50" Height="17"
                      VerticalAlignment="top" FontSize="14"/>
             <TextBlock  x:Name="txtNoteJ" Text="Note"  Foreground="Black"  Width="50" Height="17"
                        VerticalAlignment="top" FontSize="14"/>



     <StackPanel x:Name="panelMon" HorizontalAlignment="Left"  Orientation="Vertical" Width="150"
       <GridView x:Name="MonWeekJsGridView" Background="Navy"
                      Margin="0,0,0,0" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                      ItemsSource="{Binding Mode=OneWay, Source={StaticResource MonWeekJsViewSource}}"
                      ItemTemplate="{StaticResource ClassJTemplate}"
                                                        <WrapGrid Orientation="Vertical"/>



    Monday, August 5, 2013 11:54 PM


All replies

  • Hi adibu,

    This is possible by using DataTemplateSelector

    Here is a tutorial for you to know how to apply the different DataTemplates on the GirdView: http://blogs.msdn.com/b/bryanbolling/archive/2012/12/08/how-to-control-the-datatemplateselector-in-windows-store-apps.aspx

    If there is something unclear, I can provide more help,

    Best Regards,

    James He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 6, 2013 7:51 AM
  • Hi,
    Thanks for the prompt answer.

    To be more specific the logic of my project is:

    1. I have on top 2 buttons UP and Down for week number.
    2. I have 7 GridViews one for every day of the week.
    3. In the GridView Cell(Class) I have TextBlocks for Subject, Teacher, Room
    (supplied from SQLite tables for each) and start end time all are
    filled when the Class is created or updated in a SQLite table Week.
    Data are the same for any of week of the year and are loaded at start time.
    4.When Cell(Class) is clicked I add or update homework, event, and note
    in a Homework table according to the date.
    This data suppose to change for every week.

    At this point all is perfect. Now the trouble comes.

    5.To make user aware that he need to click the cell for detail I wrote the
    text : Home, Event, Note in 3 TextBlocks
    I added a 3 TextBlocks for Home, Event and Note with data is stored in Homework
    table. They must be updated every time when I click Up and Down and when project
    is loaded. Null text means no action, text = action exist.

    I don't understand how DataTemplateSelector can do this.
    From my point of view it groups data in my case can be days of the weeks and Items
    can be Cell(Class), but it don't solve the problem of dynamically update the last 3

    So I need a way to link the DataTemplate to Week table for static data and one for
    dynamic data from Homework table, or if is possible to have one DataTemplate for
    Week table and one for Homework in the same cell of the GridView.



    Tuesday, August 6, 2013 6:07 PM
  • Hi

    You have to try DataTemplateSelector for this.

    sandeep chauhan

    Wednesday, August 7, 2013 7:08 AM
  • Hi Adrian,

    Thank you for providing me a more detail information.

    I think therefore you could create a Data Model for your app, each time you click your up and down, the project renew the data model for "Home" "Event" and "Note" from your SQL Table "Homework".

    Or other community members will give you more suggestions.

    Best Regards,

    James He
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 7, 2013 7:25 AM
  • Hi,
    Thanks for the prompt answers.

    Now I am trying to do a table Class.
    I will use it to store temporary info from
    Week and Homework.

    When I start I will get the week number.
    I will create the records in Class for every day
    of the week.
    I will bind all 11 TextBlocks now without problems from
    Class not from Week.

    When I hit Up or Do I get the new week number.
    I will create in Class the new records and I will delete
    preview the records.

    What I don't know is how I manage DataContext when I have
    3 table.

    Please tell me if is possible to reach the goal or you have more

    simple ideas.



    Wednesday, August 7, 2013 1:45 PM
  • Hi,

    Can somebody to show me please how to create

    a MVVM collection and fill it with data from other?



    Wednesday, August 7, 2013 6:03 PM