WP8 ListPicker绑定 [英] WP8 ListPicker Bind

查看:110
本文介绍了WP8 ListPicker绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过项目源我的数据绑定到我的 ListPicker

When I bind my data via item source to my ListPicker:

C#:

var sightingTypes = SightingTypes.List;
sightingTypesPicker.ItemsSource = sightingTypes;



XML:

XML:

    <toolkit:ListPicker x:Name="sightingTypesPicker" ItemsSource="{Binding sightingTypes, ElementName=this}">
        <toolkit:ListPicker.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
                </StackPanel>
            </DataTemplate>
        </toolkit:ListPicker.ItemTemplate>
    </toolkit:ListPicker>



我可以看到名称被显示在ListPicker,但是,当我点击它显示了ListPicker在对象类型的列表中,这样的:

I can see the Name being shown in the ListPicker, but, when I click on the ListPicker it shows the List of the Object Type, like this:

MyProject.Model.SightingType
MyProject.Model.SightingType
MyProject.Model.SightingType
MyProject.Model.SightingType
MyProject.Model.SightingType
MyProject.Model.SightingType

我如何:

答:请在名称地产SHOW当列表显示

A: Make the Name Property show when the list shows

乙:绑定 ID 属性的值,但没有表现出来。

B: Bind the ID Property as the Value but not show it

推荐答案

您需要分配 FullModeItemTemplate 对于工作:

<toolkit:ListPicker x:Name="sightingTypesPicker" ItemsSource="{Binding sightingTypes, ElementName=this}">
    <toolkit:ListPicker.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
            </StackPanel>
        </DataTemplate>
    </toolkit:ListPicker.ItemTemplate>
    <toolkit:ListPicker.FullModeItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
                <TextBlock Text="{Binding SomeOtherProp}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
            </StackPanel>
        </DataTemplate>
    </toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>



修改:要回答你的问题B:你可以使用的SelectedItem的DependencyProperty获得所选对象的实例:

EDIT: To answer your question B: You can use the SelectedItem DependencyProperty to get the instance of the selected object:

使用MVVM方式:

<toolkit:ListPicker x:Name="sightingTypesPicker" 
                    ItemsSource="{Binding SightingTypes}"
                    SelectedItem="{Binding SelectedSigntingType, Mode=TwoWay}">

通过代码隐藏方式:

sightingTypesPicker.SelectionChanged += (s, e) => {
    MessageBox.Show(((SightingType)e.AddedItems[0]).ID);
};

这篇关于WP8 ListPicker绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆