ListPicker显示对象名称而不是属性 [英] ListPicker shows object name instead of property
问题描述
在我的模型中,我有课:
In my model I have class:
public class Heaviness
{
public int ID {get; set;}
public string NameToDisplay {get; set;}
}
在我的视图模型中,我有一个属性HeavinessList:
in my view model I have a property HeavinessList:
public ObservableCollection<Heaviness> HeavinessList {get;set;}
在我的xaml中,我添加了ListPicker,并将项源绑定到HeavinessList;其项目的数据模板已绑定到Heaviness对象的NameToDisplay属性.
In my xaml I added ListPicker with items source binded to HeavinessList; its data template for item is binded to NameToDisplay property of Heaviness object.
<StackPanel x:Name="HeavinessGroup" Width="220" HorizontalAlignment="Left">
<TextBlock x:Name="HeavinesLabel" Margin="12,0,0,0" TextWrapping="Wrap"
Text="Heaviness:" HorizontalAlignment="Left"/>
<toolkit:ListPicker x:Name="HeavinessListPicker"
SelectedIndex="0"
ItemsSource="{Binding HeavinessList}"
VerticalAlignment="Center">
<DataTemplate>
<TextBlock Text="{Binding NameToDisplay}" />
</DataTemplate>
</toolkit:ListPicker>
</StackPanel>
但是,当我运行应用程序时,显示的是对象名称而不是属性.为什么会发生?
However, when I run the app I got the object name displayed instead of property. Why does it happen?
推荐答案
您需要将DataTemplate
分配给ItemTemplate
属性,而不要直接将其放入ListPicker
:
You need to assign your DataTemplate
to the ItemTemplate
property and don't put it directly into the ListPicker
:
<toolkit:ListPicker x:Name="HeavinessListPicker"
SelectedIndex="0"
ItemsSource="{Binding HeavinessList}"
VerticalAlignment="Center">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding NameToDisplay}" />
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
您可以阅读有关 MSDN上的数据模板的更多信息(本文与WPF有关,但相同概念适用于WP7)
You can read more about DataTemplates on MSDN (the article is about WPF but the same concepts apply for WP7)
这篇关于ListPicker显示对象名称而不是属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!