动态创建绑定并将其设置为创建silverlight的字符串对象

时间:2011-09-07 10:52:05

标签: c# silverlight silverlight-4.0

我想动态创建绑定并将此绑定设置为即时创建的字符串对象,并将其绑定到组合框的displaymemberpath属性。

我该怎么做呢?

到目前为止,这是我的代码,但似乎不起作用。我将如何设置绑定的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);
    } 

1 个答案:

答案 0 :(得分:0)

您正在寻找Text属性,您可以在xaml中执行绑定:

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

修改 由于您是动态创建组合,以下是如何进行绑定:

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

编辑2: 我的坏,这是WPF。 Silverlight中没有Text属性。