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开发者_StackOverflow 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);
}
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.
精彩评论