【发布时间】:2014-10-20 22:16:12
【问题描述】:
我正在尝试在 wpf-Listview 中显示对象的集合,并提供一个组合框以从其他集合中选择值。
一些代码来说明我的问题:
XAML: (Window gets its DataContext set in app.xaml.cs)
<ListView ItemsSource="{Binding Path=Books}" SelectedItem="{Binding Path=CurrentBook}">
<ListView.View>
<GridView>
<GridViewColumn Header="author" DisplayMemberBinding="{Binding author}" />
<GridViewColumn Header="title" DisplayMemberBinding="{Binding title}" />
<GridViewColumn Header="genre">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Genres}" IsSynchronizedWithCurrentItem="true" DisplayMemberPath="genreName" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CurrentBook_Genre}">
<ComboBox.ItemTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding genreName}" />
<TextBlock Text="{Binding genreDescription}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
目前“CurrentBook_Genre”是 MainWindowViewModel 的一个 Property,ListView 中显示的 Item 的相应值由它的 setter 设置。
就像在 ListView 的每一“行”中显示相同的 ComboBox。 我想我需要某种“EditValueTemplate”和“ShowValueTemplate”以及某种选择器,如果 ComboBox 仅显示在处于编辑模式的行中,那会很好 - 我想这是常见的事情大多数 DataDriven 应用程序,所以也许有一种我不知道的更简单的方法。
对此唯一好的解释是 http://tech.pro/tutorial/857/wpf-tutorial-using-the-listview-part-3-in-place-edit 但他们正在使用依赖属性,这与我正在使用的 ViewModel 包装模型相反。
【问题讨论】:
-
他们正在使用依赖属性,与我使用的 ViewModel 包装模型相反...
DependencyProperty不反对一个普通的属性,当涉及到Binding Paths 时,它们的工作方式相同。如果您无法调整找到的解决方案来解决您自己的问题,那么您作为开发人员就会遇到麻烦。 -
我现在正在阅读它们,感谢您的关注。每天都有新的概念和名称——如果我了解基本的工具和概念,它应该会变得更好。
-
听起来您的问题是由于您将
MainWindowViewModel类中的单个属性数据绑定到GridView中每个ComboBox的ComboBox.SelectedItem(或类似)属性而引起的。解决方案是将该属性的副本添加(或将其移动)到Book类中,这样每个Book项目将拥有自己的属性,并知道在其自己的ComboBox中选择了哪个项目。 -
再次感谢(顺便说一句,您在 s.o. 上所做的所有工作)- 我需要花一些时间来尝试并重新表述这个问题。
标签: wpf xaml listview mvvm combobox