【问题标题】:mvvm ComboBox selection in ListviewListview 中的 mvvm ComboBox 选择
【发布时间】: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 中每个ComboBoxComboBox.SelectedItem(或类似)属性而引起的。解决方案是将该属性的副本添加(或将其移动)到Book 类中,这样每个Book 项目将拥有自己的属性,并知道在其自己的ComboBox 中选择了哪个项目。
  • 再次感谢(顺便说一句,您在 s.o. 上所做的所有工作)- 我需要花一些时间来尝试并重新表述这个问题。

标签: wpf xaml listview mvvm combobox


【解决方案1】:

与您找到的示例相比,有一种更简单的方法可以在编辑网格行时显示一个控件,而在未编辑网格行时显示另一个控件。试试这个:

<DataGrid ItemsSource="{Binding Books}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CurrentBook_Genre.genreName}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <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}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

当正在编辑该行时,这将显示一个 ComboBox,而当它没有被编辑时,将显示一个普通的旧 TextBlock

【讨论】:

    猜你喜欢
    • 2014-08-07
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2020-08-04
    • 2017-12-31
    相关资源
    最近更新 更多