MVVM WPF ComboBox SelectedValue 不是确切的值



我正试图在DataGrid.RowDetailsTemplate 中实现一个组合框

这是组合框:

<ComboBox
Grid.Column="1"
Width="300"
BorderThickness="1"
DisplayMemberPath="Name"
Foreground="{StaticResource BrushTextForeground}"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=DataContext.Categories, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
SelectedValue="{Binding Path=Category, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

Categories是Category:的IEnumerable列表

public IEnumerable<Category> Categories { get; set; }

Category是事务列表中的一个属性,也是我的Datagrid:的项源

private readonly ObservableCollection<Transaction> _transactions;
public IEnumerable<Transaction> Transactions => _transactions;

问题是,当我打开数据网格的行时,组合框不接受selectedValue Category的值,而是只接受项目的第一个项目此外,当我从组合框中选择一个值时,会为数据网格中的所有行选择相同的值。

考虑到您的类别列表是Categories,您之前在ViewModel中设置了SelectedCategory,并且您的类别Category具有属性Name,我将修改如下。

<ComboBox 
ItemsSource="{Binding Path=Categories}" 
SelectedItem="{Binding Path=SelectedCategory}"
//Mode=TwoWay, UpdateSourceTrigger=PropertyChanged not necessary
DisplayMemberPath="Name" Margin="5"
/>

最新更新