locked
Binding control to a list inside parents datacontext

    Question

  • I have the following control

    <ListView x:Name="MyMeetsRow"  Grid.Column="0" ItemsSource="{Binding Source={StaticResource cvsActivities}}" Style="{StaticResource MyMeetingsRowUsers}" GroupStyleSelector="{StaticResource listGroupStyleSelector}" />

    with these styles:

    <DataTemplate x:Key="MyMeetingUser">
        <Grid Margin="0,0,0,10" >
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <StackPanel Height="78" Width="875">
                <StackPanel.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0,1"  >
                        <GradientStop Color="#FF0072C6" Offset="0"/>
                        <GradientStop Color="#FF008FD4" Offset="1"/>
                    </LinearGradientBrush>
                </StackPanel.Background>
                <Grid >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="90"/>
                        <ColumnDefinition Width="65"/>
                        <ColumnDefinition Width="388"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
    
                    <TextBlock x:Name="MyMeetingRowDayNumber" Grid.Column="0" Text="{Binding MyMeetingRowDayNumber}" Style="{StaticResource MyMeetingsRowDay}"   />
                    <Grid Grid.Column="1">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" x:Name="MyMeetingRowDayText" Text="{Binding MyMeetingRowDayText}" Style="{StaticResource MyMeetingsRowDayText}" Height="28"></TextBlock>
                        <TextBlock Grid.Row="1" x:Name="MyMeetingRowDayHour" Text="{Binding MyMeetingRowDayHour}" Style="{StaticResource MyMeetingsRowDayHour}" ></TextBlock>
                    </Grid>
                    <TextBlock Grid.Column="2" x:Name="MyMeetingsRowTitle" Text="{Binding MyMeetingsRowTitle}" Style="{StaticResource MyMeetingsRowTitle}" />
                    <!--<ScrollViewer Grid.Column="3" HorizontalScrollBarVisibility="Auto" DataContext="{Binding Item}" VerticalScrollBarVisibility="Disabled" HorizontalScrollMode="Auto" VerticalScrollMode="Disabled">
                        <GridView DataContext="{Binding Invitees}">
                            <GridView.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding status}"/>
                                </DataTemplate>
                            </GridView.ItemTemplate>
                        </GridView>
                    </ScrollViewer>-->
                </Grid>
            </StackPanel>
        </Grid>
    </DataTemplate>
    
    <GroupStyle x:Key="listViewGroupStyle">
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <Grid Background="#8e8e8e" Width="805" Height="75" Margin="0,0,0,13">
                    <TextBlock Text='{Binding Key}' Foreground="White" Margin="10,10,0,0" FontFamily="Segoe UI Light" FontSize="55" FontWeight="Light" FontStyle="Normal"/>
                </Grid>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
    
    <Style x:Key="MyMeetingsRowUsers" TargetType="ListView" >
        <Setter Property="HorizontalContentAlignment" Value="Right" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="TabNavigation" Value="Local"/>
        <Setter Property="IsHoldingEnabled" Value="True"/>
        <Setter Property="Margin" Value="44,36,0,0" />
        <Setter Property="SelectionMode" Value="None" />
        <Setter Property="ItemTemplate" Value="{StaticResource MyMeetingUser}" />
        <Setter Property="Transitions">
            <Setter.Value>
                <TransitionCollection>
                    <EntranceThemeTransition />
                </TransitionCollection>
            </Setter.Value>
        </Setter>
    </Style>

    and the items im binding to it is this List

        private ObservableCollection<MeetingRow<Meeting>> myMeet = new ObservableCollection<MeetingRow<Meeting>>();
    
    ...
    
    var result = from m in MyMeets group m by m.FullMonth into grp orderby grp.Key select grp;
                cvsActivities.Source = result;

    as for the class Meeting row, it looks like this:

    public class MeetingRow<T> : BindableBase
    {
        private T item;
        public T Item
        {
            get { return item; }
            set { SetProperty(ref item, value); }
        }
    
        private string myMeetingRowDayNumber;
        public string MyMeetingRowDayNumber
        {
            get { return myMeetingRowDayNumber; }
            set { SetProperty(ref myMeetingRowDayNumber, value); }
        }
    
        private string myMeetingRowDayText;
        public string MyMeetingRowDayText
        {
            get { return myMeetingRowDayText; }
            set { SetProperty(ref myMeetingRowDayText, value); }
        }
    
        private string myMeetingRowDayHour;
        public string MyMeetingRowDayHour
        {
            get { return myMeetingRowDayHour; }
            set { SetProperty(ref myMeetingRowDayHour, value); }
        }
    
        private string myMeetingsRowTitle;
        public string MyMeetingsRowTitle
        {
            get { return myMeetingsRowTitle; }
            set { SetProperty(ref myMeetingsRowTitle, value); }
        }
    
        private string fullMonth;
        public string FullMonth
        {
            get { return fullMonth; }
            set { SetProperty(ref fullMonth, value); }
        }
    }

    and my Meeting Class

    public class Meeting
    {
    ...
        public List<MeetingInvitee> Invitees { get; set; }
    }

    after i fill my cvsActivities variable to put in the ListView ItemSource it displays the info correctly, but now i need to access the List<MeetingInvitee> inside the Item parameter in each MeetingRow that is on the cvsActivities List to fill the commented ScrollViewer. Basically i want to access a List inside each item on the parent ItemSource.  

    Monday, May 11, 2015 11:59 AM

Answers

  • >>but now i need to access the List<MeetingInvitee> inside the Item parameter in each MeetingRow that is on the cvsActivities List to fill the commented ScrollViewer. Basically i want to access a List inside each item on the parent ItemSource.

    The MeetingRow has an Item property that returns the Meeting object so if your ListView is bound to a collection of MeetingRow objects and you want to display the MeetingInvitee objects for each Meeting you could use an inner ListBox, i.e. add an ItemsControl to the MyMeetingUser template and bind its ItemsSource property to Item.Invitees:

    <DataTemplate x:Key="MyMeetingUser">
        <Grid Margin="0,0,0,10" >
    ...
    
    <ListView Grid.Column="3" Grid.Column="0" ItemsSource="{Binding Item.Invitees}"
    ScrollViewer.VerticalScrollBarVisibility="Hidden">
    <ListView.ItemTemplate>
    <DataTemplate>
    <TextBlock Text="{Binding status}"/>
    </DataTemplate>
    </ListView>
    
                </Grid>
            </StackPanel>
        </Grid>
    </DataTemplate>
    

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

     

    • Marked as answer by Thought2 Monday, May 11, 2015 4:52 PM
    Monday, May 11, 2015 12:50 PM