gpt4 book ai didi

wpf - 在 ListView 中选择mvvm ComboBox

转载 作者:行者123 更新时间:2023-12-03 10:46:09 25 4
gpt4 key购买 nike

我正在尝试在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的一个属性,ListView中显示的Item的相应值由其setter设置。

像这样,相同的ComboBox显示在ListView的每个“行”中。
我想我需要某种“EditValueTemplate”和“ShowValueTemplate”以及某种选择器,如果ComboBox仅显示在处于编辑模式的行中,那就很好了-我猜这对于大多数DataDriven应用程序,所以iam可能不知道有一种更简单的方法。

对此的唯一好的解释是
http://tech.pro/tutorial/857/wpf-tutorial-using-the-listview-part-3-in-place-edit
但是他们使用的是依赖属性,而不是iam使用的ViewModel封装模型。

最佳答案

与找到的示例相比,有一种更容易的方法来使一个网格行被编辑时显示一个控件,而当一个网格行未被编辑时显示另一个控件。尝试以下方法:

<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

关于wpf - 在 ListView 中选择mvvm ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25525287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com