1

I am populating my WPF ComboBox like this

foreach (Function fx in XEGFunctions.GetAll())
{
    ComboBoxItem item = new ComboBoxItem();
    item.Content = fx.Name;
    item.ToolTip = fx.Signature;               
    //item.( some property ) = fx.FunctionValue;
    cmbBoxTransformation.Items.Add(item);
}
cmbBoxTransformation.SelectedIndex = 0;

How can I set some different value to each ComboBoxItem.

4 Answers 4

5

If the value you're looking to set is only used in the back end, and not displayed to the user, the Tag property is probably your best bet.

item.Tag = fx.FunctionValue;
Sign up to request clarification or add additional context in comments.

3 Comments

But If I am having value with me I want to show corresponding Item selected at runtime whose value is this (some text) then i think it will not work
You can use a linq query to find it pretty quickly, I'm not entirely clear as to what you're trying to do from your question though
Ok it's good I will iterate through a LINQ query to select a particualar Item whose value I m having at runtime
2

Two options

  1. You can create derived type from ComboBoxItem, and define properties in derived type.

  2. You can create arbitrary collection of item (with your custom properties), and set ComboBox.ItemsSource to that collection, and DisplayMemberPath to the field that needs to be displayed in the Combobox.

Binding combobox to display source and binding source

How SelectedValue and DisplayMemberPath saved my life

Comments

1

this little tick may helps someone

<ComboBox SelectedIndex="1" SelectedValuePath="Tag"  SelectedValue="{Binding SampleDept,Mode=OneWayToSource}" >
                                <ComboBoxItem Content="8-bit" Tag="8"  ></ComboBoxItem>
                                <ComboBoxItem Content="16-bit" Tag="16" ></ComboBoxItem>
                                <ComboBoxItem Content="24-bit" Tag="24"></ComboBoxItem>
                                <ComboBoxItem Content="32-bit" Tag="32"></ComboBoxItem>
                            </ComboBox>
public class SampleModel{

   public int SampleDept{

            get { return _sampleDept; }
            set {
                
                _sampleDept = value;
            OnPropertyChanged("SampleDept");
            }
        }
}

Comments

0
var listItems = val.Split('$');
DataTemplate dt = new DataTemplate();
var combo = new FrameworkElementFactory(typeof(ComboBox));
combo.SetValue(ComboBox.ItemsSourceProperty, listItems);
combo.SetValue(ComboBox.SelectedValueProperty, "Whatever");
combo.SetBinding(ComboBox.SelectedValueProperty, new Binding("Value") { Source = mkvc });
dt.VisualTree = combo;
dt.Seal();

add this to editor template of whatever you want to add combobox to it => mkvc is a class for holding my data

PropertyDefinition pd = new PropertyDefinition();
pd.EditorTemplate = dt;
//rpg =>radPropertyGrid
rpg.PropertyDefinitions.Add(pd);
rpg.Item = propertyList;

propertylist is list of myclass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.