Reputation: 4298
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!
Upvotes: 1
Views: 4169
Reputation: 12073
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.
Upvotes: 3
Reputation: 8849
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}"
Upvotes: 2