当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Select ComboBoxItem based on its tag value

问题描述:

My ComboBoxItems have string values that represent the name of the item to be selected, but I've stored the value for that item in Tag property of the ComboBoxItem. I use the Tag as the asp.net equivalent to the dropdownlist Item Value.

In the code behind where I set the datagrid template column, I set the ComboBoxItem as follows:

<ComboBoxItem Tag='" + product.ProductGuid + "' Content='" + product.Name + "'></ComboBoxItem>

I need to programmatically select a ComboBoxItem based on the Tag value not the content. In the code below, currentProduct holds the ProductGuid value that I need to select, but this code will select the ComboBoxItem whose Content is my currentProduct

((ComboBox)QuotationDG.Columns[0].GetCellContent(MyData[MyData.Count - 1])).SelectedValue = currentProduct;

Is there a way to set the ComboBox Selected value to the ComboBoxItem whose Tag value is currentProduct?

EDIT:

Here's the code I use to bind my ComboBox Column:

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();

}

网友答案:

What you need is

YourComboBox.SelectedValue = currentProduct

and

<ComboBox SelectedValuePath="Tag" etc. etc. etc. />

which means your SelectedValue gets its value from your Tag

网友答案:

The use of the combobox has the ability bind to objects and specify what is to be displayed in the dropdown. Why not use the Product object which you are loading into the tag but show something different on the drop down?

If need be create an extended, Partial object, to display something wholly different in the drop down, but still access the Product item to change the selection.


The suggestion here is to simply bind the combobox to the Product data item. Then you can change the value dynamically without using the Tag property.

For example I have this data type similar to your Product type:

public class PriceItem
{
    public string Name { get; set; }
    public int Price { get; set; }
}

and here is the data list of values which is saved on my VM as Items.

Items = new List<PriceItem>()
{
    new PriceItem() { Name = "Alpha", Price=100 },
    new PriceItem() { Name = "Beta",  Price=200 },
};

Scenario Two combo boxes, both bound to the Items data. One combo box shows the Name in its dropdown while the other shows the Price. The Name combobox controls the price combo box, when it changes the price changes as well.

<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}" />

In action both combo boxes are blank. But whenever I select the first one it changes the second. All using the same data/data objects.

分享给朋友:
您可能感兴趣的文章:
随机阅读: