简体   繁体   English

WPF 中 Datagrid 内的组合框选定项绑定不起作用

[英]Combobox selected item binding inside Datagrid in WPF not working

I am having a issue with a Combo box inside a datagrid in WPF.我在 WPF 中的数据网格中遇到了组合框问题。 I want the arrow on the combo box to be visible even when it is not in editing mode.我希望组合框上的箭头即使不在编辑模式下也可见。 I couldn't achieve this behavior with DataGridComboBoxColumn which otherwise was working fine.我无法使用 DataGridComboBoxColumn 实现这种行为,否则它可以正常工作。 To fix this appearance issue I had to use normal combo box.为了解决这个外观问题,我不得不使用普通的组合框。

 <DataGridTemplateColumn Header="Parameter Group" MinWidth="150" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox  ItemsSource="{Binding Source={StaticResource GroupList}}"
                                            DisplayMemberPath="ParameterGroupName"
                                           IsSynchronizedWithCurrentItem="True"
                                           SelectedValuePath="ParameterGroupName"
                                           SelectedValue="{Binding Path=ParameterGroup,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                           > 
                                </ComboBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

Now the problem is that the selected item binding is not working.现在的问题是所选项目绑定不起作用。 Any item selected for a row is applying for all.为一行选择的任何项目都适用于所有项目。 I am not sure what's wrong here.我不确定这里出了什么问题。

The item source is-项目来源是-

private ObservableCollection<ParameterGroupModel> _parameterGroupList;

        public ObservableCollection<ParameterGroupModel> ParameterGrpList
        {
            get
            {
                return _parameterGroupList;
            }
            set
            {
                _parameterGroupList = value;
                NotifyPropertyChanged("ParameterGrpList");
            }
        }

And the selected value is a simple string inside the model.所选值是模型内的一个简单字符串。 Can someone please help?有人可以帮忙吗?

With the CellTemplate you 'duplicate" the same xaml code in each cell at running time, thus the same bindings. So each cell refers to the same datasource and the same selectedItem object.使用 CellTemplate,您可以在运行时在每个单元格中“复制”相同的 xaml 代码,因此具有相同的绑定。因此每个单元格引用相同的数据源和相同的 selectedItem 对象。

You must define somewhere a collection of object on which each row can bind separately its selected item and refer to it specifically in each cell (one possible solution may be to use a multibinding with the selectedItemCollection and the row number for exemple to determine which item of the selectedItemCollection your row has to bind to)您必须在某处定义一个对象集合,在该集合上每一行都可以单独绑定其所选项目,并在每个单元格中专门引用它(一种可能的解决方案可能是使用带有 selectedItemCollection 和行号的多重绑定,例如,确定哪个项目是您的行必须绑定到的 selectedItemCollection)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM