简体   繁体   中英

Data template and steady item in ListPicker

I am wondering if i can create a ListPicker which is combined together from Items taken from Data Template and one item in the bottom that is steady, and is always there.

Here is my code:

<toolkit:ListPicker 
    x:Name="subjectTeacherListPicker" 
    ItemsSource="{Binding AllTeachers}"
    Grid.Column="1" 
    Grid.Row="5">

    <toolkit:ListPicker.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                 <Run Text="{Binding TeacherName}"/>
                 <Run Text="{Binding TeacherSurname}"/>
            </TextBlock>
        </DataTemplate>
    </toolkit:ListPicker.ItemTemplate>

</toolkit:ListPicker>

I mean, to the code above i'd like to add one item to the list picker that will be shown in the same way as all the items but will be settled one time and will stay there no matter what. Like for example, "Add new teacher..", that will be viewed even no other item will be shown.

So, the problem is you cannot bind different object types to an itemSource so you cannot do this using the ListPicker control. There is a control that is overlooked alot called ItemsControl . This can have items bound to it and it creates a non-scrolling stacked list of the bound objects. Combining this control with a ScrollViewer and a StackPanel will give the desired results.

1

This is scrollable with the button you need on the bottom of the list. it will always be there even when there is no data in the list.

The XAML to make this happen is as follows:

<ScrollViewer Margin="5">
    <StackPanel Orientation="Vertical">
        <ItemsControl ItemsSource="{Binding AllTeachers}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock>
                            <Run Text="{Binding TeacherName}"/>
                            <Run Text="{Binding TeacherSurname}"/>
                    </TextBlock>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    <Button Content="Add teacher"/>                    
    </StackPanel>
</ScrollViewer>

Replacing your ListPicker with the code I provided will give you a really great starting point to finish customizing the ItemTemplate to get the desired item look.

If you need the "Add Teacher" to be some other control, such as a TextBlock , simply change the control and have it trigger the Add Teacher action.

Hope this helps!

Adam

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM