ComboBox.SelectedItem绑定

时间:2011-07-27 16:06:11

标签: c# silverlight silverlight-4.0 mvvm mvvm-light

我在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>

然后这是后面的代码(我已经删除了不相关的代码):

            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");                        
                }
            }
        }
        }

问题在于:DataGrid单元格有2个模板:CellTemplate和CellEditingTemplate。当CellTemplate处于活动状态时,其中的文本框将按计划选择SelectedProvince并显示该省的名称。问题是,当CellEditingTemplate变为活动状态时,其中的ComboBox不会拾取(默认)SelectedItem值并显示一个空框。

我有什么遗失的吗?如何设置绑定,以便可以在CellEditingTemplate中的组合框中设置默认的SelectedItem?

提前多多谢谢!

2 个答案:

答案 0 :(得分:3)

我认为ItemsSource的TwoWay绑定可能是问题所在。

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

我建议根据您的设计将其更改为OneTimeOneWay

答案 1 :(得分:2)

我可能错了,但我认为如果您使用的是SelectedValuePath,则需要使用SelectedValue代替SelectedItem,所以请更改:

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

到此:

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