当我通过项目来源将我的数据绑定到我的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>
我可以看到List在ListPicker中显示,但是,当我点击ListPicker时,它会显示对象类型的列表,如下所示:
MyProject.Model.SightingType
MyProject.Model.SightingType
MyProject.Model.SightingType
MyProject.Model.SightingType
MyProject.Model.SightingType
MyProject.Model.SightingType
我如何:
答:列表显示
时显示Name
属性
B:将ID
属性绑定为值但不显示
答案 0 :(得分: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);
};