简体   繁体   中英

Combobox selected item binding inside Datagrid in WPF not working

I am having a issue with a Combo box inside a datagrid in 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. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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