ComboBox.SelectedItem 绑定
我的 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?
提前非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 ItemsSource 的 TwoWay 绑定可能是问题所在。
ItemsSource="{Binding Path=ProvinceList, Mode=TwoWay}"
我建议根据您的设计将其更改为
OneTime
或OneWay
。I think TwoWay binding for ItemsSource can be the problem.
ItemsSource="{Binding Path=ProvinceList, Mode=TwoWay}"
I suggest to change it to
OneTime
orOneWay
depending on your design.我可能是错的,但我认为如果您使用
SelectedValuePath
,则需要使用SelectedValue
而不是SelectedItem
,因此更改此:对此:
I might be wrong, but I think that if you're using
SelectedValuePath
, you need to useSelectedValue
instead ofSelectedItem
, so change this:to this: