我有此列/代碼在我的DataGrid:ComboBox.SelectedItem綁定
<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?
非常感謝!
我已經刪除了行SelectedValuePath =「Province2Code」不要混淆任何人。 – Andrei
+1將ItemsSource的模式設置爲OneWay - 修復它! =))謝謝! 我認爲你應該讓它成爲答案。 – Andrei