简体   繁体   中英

ComboBox.SelectedItem binding

I have this column/code in my DataGrid:

<sdk:DataGridTemplateColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="Province/State">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=SelectedProvince.ProvinceName, Mode=OneWay}"/>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                    <sdk:DataG  ridTemplateColumn.CellEditingTemplate>                        
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=ProvinceList, Mode=TwoWay}"
                                      SelectedItem="{Binding Path=SelectedProvince, Mode=TwoWay}"
                                      DisplayMemberPath="ProvinceName" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellEditingTemplate>
                </sdk:DataGridTemplateColumn>

Then this is the code behind(I have cut out the unrelated code):

            public class BatchSeedingAddressVM : ViewModelBase
            {
                public BatchSeedingAddressVM()
                {
                    _saveAddressButtonCommand = new RelayCommand(SaveAddressButtonCommand_OnExecute);

                    CreateJurisdictionList();
                }

                public BatchSeedingAddressVM(int? batchSeedingAddressOID, string address1, string address2, string city, string postalCode, string province2Code)
                {
                    _saveAddressButtonCommand = new RelayCommand(SaveAddressButtonCommand_OnExecute);
                    CreateJurisdictionList();

                    BatchSeedingAddressOID = batchSeedingAddressOID;
                    Address1 = address1;
                    Address2 = address2;
                    City = city;
                    PostalCode = postalCode;
                    //SelectedProvince2Code = province2Code;
                    SelectedProvince = _provinceList.Where(x => x.Province2Code == province2Code).FirstOrDefault();
                }


                private ObservableCollection<Province> _provinceList = new ObservableCollection<Province>();
                public ObservableCollection<Province> ProvinceList
                {
                    get
                    {
                        return _provinceList;
                    }
                    set
                    {
                        if (_provinceList != value)
                        {
                            _provinceList = value;
                            RaisePropertyChanged("ProvinceList");
                        }
                    }
                }

        private Province _selectedProvince;
        [Display(Name = "Province")]
        public Province SelectedProvince
        {
            get
            {
                return _selectedProvince;
            }
            set
            {
                if (_selectedProvince != value && value != null)
                {
                    _selectedProvince = value;                            
                    RaisePropertyChanged("SelectedProvince");                        
                }
            }
        }
        }

Here is the issue: the DataGrid cell has 2 templates: CellTemplate and CellEditingTemplate. When the CellTemplate is active the textbox in it picks up the SelectedProvince as planned and displays the name of the province. The problem is that when CellEditingTemplate becomes active the ComboBox in it does not pick up the (default)SelectedItem value and displays an empty box.

Is there something I am missing? How the binding has to be setup so that it would be possible to set the default SelectedItem in the combobox in CellEditingTemplate?

Thanks much in advance!

I think TwoWay binding for ItemsSource can be the problem.

ItemsSource="{Binding Path=ProvinceList, Mode=TwoWay}"

I suggest to change it to OneTime or OneWay depending on your design.

I might be wrong, but I think that if you're using SelectedValuePath , you need to use SelectedValue instead of SelectedItem , so change this:

SelectedItem="{Binding Path=SelectedProvince, Mode=TwoWay}"

to this:

SelectedValue="{Binding Path=SelectedProvince, Mode=TwoWay}"

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