My ComboBoxItems具有表示要选择的项目名称的字符串值,但我已将该项目的值存储在ComboBoxItem
的Tag
属性中。我使用Tag
作为相当于下拉列表项目Value
的asp.net。根据其标记值选择ComboBoxItem
在后面,我将DataGrid模板列的代码,我设置了ComboBoxItem
如下:
<ComboBoxItem Tag='" + product.ProductGuid + "' Content='" + product.Name + "'></ComboBoxItem>
我需要根据Tag
值不是内容以编程方式选择ComboBoxItem
。在下面的代码,currentProduct
持有ProductGuid值,我需要选择,但是这个代码将选择其内容的ComboBoxItem
是我currentProduct
((ComboBox)QuotationDG.Columns[0].GetCellContent(MyData[MyData.Count - 1])).SelectedValue = currentProduct;
有没有一种方法来设置ComboBox
选中的值给ComboBoxItem
其标签值是currentProduct
?
编辑:
下面是我用绑定我的组合框柱代码:
private string CreateDDLColumnEditTemplate(int index, string propertyName, List<Product> ProductList)
{
StringBuilder CellTemp = new StringBuilder();
CellTemp.Append("<DataTemplate ");
CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
CellTemp.Append("2006/xaml/presentation' ");
CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");
CellTemp.Append(String.Format("<ComboBox SelectedValue='{{Binding [{0}], Mode=TwoWay}}'>",0));
foreach (Product product in ProductList)
{
CellTemp.Append("<ComboBoxItem Tag='"+product.ProductGuid+"' Content='" + product.Name + "'></ComboBoxItem>");
}
CellTemp.Append(String.Format("</ComboBox>"));
CellTemp.Append("</DataTemplate>");
return CellTemp.ToString();
}
这些答案是否适合您? – psoshmo
你在找什么是'ComboBox.SelectedValuePath'和'ComboBox.SelectedValue'。这两个相结合将做你的尝试。 – Martin