我的ComboBoxItems具有表示要选择的项目名称的字符串值,但我已将该项目的值存储在Tag
的{{1}}属性中。我使用ComboBoxItem
作为asp.net等效于下拉列表项Tag
。
在我设置datagrid模板列的代码中,我按如下方式设置Value
:
ComboBoxItem
我需要以编程方式根据<ComboBoxItem Tag='" + product.ProductGuid + "' Content='" + product.Name + "'></ComboBoxItem>
值而非内容选择ComboBoxItem
。在下面的代码中,Tag
包含我需要选择的ProductGuid值,但此代码会选择内容为currentProduct
ComboBoxItem
currentProduct
有没有办法将((ComboBox)QuotationDG.Columns[0].GetCellContent(MyData[MyData.Count - 1])).SelectedValue = currentProduct;
选定值设置为标记值为ComboBox
的{{1}}?
编辑:
这是我用来绑定我的ComboBox列的代码:
ComboBoxItem
答案 0 :(得分:3)
您需要的是
YourComboBox.SelectedValue = currentProduct
和
<ComboBox SelectedValuePath="Tag" etc. etc. etc. />
表示您的SelectedValue
从Tag
答案 1 :(得分:2)
组合框的使用能够绑定到对象并指定下拉列表中显示的内容。为什么不使用要加载到标记中的Product
对象,但在下拉列表中显示不同的内容?
如果需要创建扩展的Partial
对象,要在下拉列表中显示完全不同的内容,但仍然可以访问 Product
项以更改选择。< / p>
这里的建议是简单地将组合框绑定到Product
数据项。然后,您可以动态更改值,而无需使用Tag
属性。
例如,我的数据类型类似于您的产品类型:
public class PriceItem
{
public string Name { get; set; }
public int Price { get; set; }
}
以下是我的VM上保存为Items
的值的数据列表。
Items = new List<PriceItem>()
{
new PriceItem() { Name = "Alpha", Price=100 },
new PriceItem() { Name = "Beta", Price=200 },
};
<强>方案强>
两个组合框,都绑定到Items
数据。一个组合框在其下拉列表中显示Name
,而另一个显示Price
。 Name
组合框控制价格组合框,同时也改变价格变化。
<ComboBox x:Name="comboName" ItemsSource="{Binding Items}" DisplayMemberPath="Name" />
<ComboBox x:Name="comboValue"
ItemsSource="{Binding Items}"
DisplayMemberPath="Price"
SelectedValuePath="Price"
SelectedValue="{Binding SelectedItem.Price, ElementName=comboName, Mode=OneWay}" />
在操作中,两个组合框都是空白的。但每当我选择第一个时,它就会改变第二个。全部使用相同的数据/数据对象。