1

I wanted to create binding dynamically and set this binding to a string object that was created on-the-fly and bind this to the displaymemberpathproperty of a combo box.

How do I go about doing this?

Here is my code so far but doesn't seem to work. What will I be setting the path property of the binding to (i.e. the reason i'm doing it this way is cause I have number of combo boxes that are using this one method):

    private void ComboValue_DropDownClosed(object sender, EventArgs e)
    {
        ComboBox combo = (ComboBox)sender;
        int selectedItemCount = 0;
        foreach (MyItem item in combo.Items)
        {
            if (item.IsSelected == true)
                selectedItemCount = selectedItemCount + 1;
        }
        string SelectedComboCount = selectedItemCount.ToString();
        Binding b = new Binding();
        b.Source = SelectedComboCount ;
        combo.SetBinding(ComboBox.DisplayMemberPathProperty, b);
    } 
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Mike
  • 21
  • 3
  • 1
    +1 Because I hate to see newbies be downvoted without being given a chance. Normally it is possible to make sense of a question written by even those with very poor English skills (due to English not being their native language). However I'm finding the text in this question very difficult. Could you consider including some full stops to break the large single sentence into smaller sentences. – AnthonyWJones Sep 07 '11 at 11:56
  • @AnthonyWJones: Have done my best to clean it up... poor newbies :) – iCollect.it Ltd Sep 07 '11 at 14:03

1 Answers1

0

You are looking for the Text property and you can do the binding in xaml:

<ComboBox Name="cb">
      ItemsSource="{StaticResource myCities}" 
      Text="{Binding ElementName=cb, Path=Items.Count}">
</ComboBox>

Edit: Since you're creating the combos dynamically, here's how to do the binding:

Binding binding = new Binding();
binding.Source = combo;
binding.Path = new PropertyPath("Items.Count");
combo.SetBinding(ComboBox.TextProperty, binding);

Edit 2: My bad, this is for WPF. The Text property is not available in Silverlight.

alf
  • 18,372
  • 10
  • 61
  • 92
  • Hi I cant do the binding on the xaml the combos are made dynamically, the user clicks and creates multi select combo boxes and this is all done dynamically on the code behind. – Mike Sep 07 '11 at 12:44
  • Ok, I added an example showing how to create the binding dynamically. Hope it helps! – alf Sep 07 '11 at 13:43
  • Hi Alfonso thanks for the reply, the property path when you set it, im guessing Items is the class name? can you describe what you giving as the path, also there is no comboBox.TextProperty in silverlight – Mike Sep 07 '11 at 14:12
  • Ah, sorry, the Text property is only available in WPF, not Silverlight. This solution won't work, my bad :( – alf Sep 07 '11 at 15:24