【发布时间】:2015-04-12 03:32:21
【问题描述】:
我正在处理在我的 WPF 应用程序的某种列表中显示的食谱列表。 我有一系列食谱
public Cookbook()
{
RecipeList=new ObservableCollection<Recipe>();
AddRecipe(new Recipe("Food1", 0, null));
}
每个配方都有一个名为 Name.public string Name { get; set; }
我现在正在做的是用这个集合填充列表
<ListView x:Name="CategoriesListBox" Margin="10,0,10,0" ItemsSource="{Binding RecipeList}"
Loaded="CategoriesListBox_OnLoaded"
SelectionChanged="CategoriesListBox_SelectionChanged">
<ListBox.DataContext>
<Implementation:Cookbook/>
</ListBox.DataContext>
</ListView>
这当然会导致列表中填充了对象名称 - 我想要列表中的配方名称。有没有办法在列表框中显示属性名称?
(我正在寻找 XAML 解决方案 - 没有代码)
// 我已经尝试过使用 ListView 和嵌套的 Gridview 作为解决方案 - 这可行,但这也会在顶部创建不必要的网格和标题字段。
<ListView x:Name="CategoriesListBox" Margin="10,0,10,0" ItemsSource="{Binding RecipeList}"
Loaded="CategoriesListBox_OnLoaded"
SelectionChanged="CategoriesListBox_SelectionChanged">
<ListBox.DataContext>
<Implementation:Cookbook/>
</ListBox.DataContext>
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding Path=Name, Mode=OneWay}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
谢谢
【问题讨论】: