简体   繁体   English

ListPicker中的数据模板和稳定项

[英]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. 我想知道我是否可以创建一个ListPicker,它从数据模板中获取的项目和底部的一个稳定的项目组合在一起,并且始终存在。

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. 因此,问题是您无法将不同的对象类型绑定到itemSource,因此您无法使用ListPicker控件执行此操作。 There is a control that is overlooked alot called ItemsControl . 有一个被忽略的控件叫做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. 将此控件与ScrollViewerStackPanel将得到所需的结果。

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: 实现这一目标的XAML如下:

<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. 用我提供的代码替换ListPicker将为您提供一个非常好的起点,可以完成自定义ItemTemplate以获得所需的项目外观。

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. 如果您需要“添加教师”作为其他控件(例如TextBlock ,只需更改控件并使其触发添加教师操作。

Hope this helps! 希望这可以帮助!

Adam 亚当

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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