[英]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. 我想动态创建绑定,并将此绑定设置为动态创建的字符串对象,并将其绑定到组合框的displaymemberpathproperty。
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): 我将绑定的path属性设置为什么(即,我这样做的原因是因为我有许多使用此方法的组合框):
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: 您正在寻找Text属性,并且可以在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. 编辑2:我不好,这是WPF。 The Text property is not available in Silverlight. Text属性在Silverlight中不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.