1

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

C#:

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

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 时,它会显示对象类型的列表,如下所示:

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

我如何能:

A:Name在列表显示时显示属性

B:将属性绑定ID为值但不显示

4

1 回答 1

3

您需要分配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 来获取所选对象的实例:

使用 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);
};
于 2013-08-18T19:09:20.843 回答