简体   繁体   中英

creating a binding dynamically and setting it to a string object that was created silverlight

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM