繁体   English   中英

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

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

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

我该怎么做呢?

到目前为止,这是我的代码,但似乎没有用。 我将绑定的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);
    } 

您正在寻找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。 Text属性在Silverlight中不可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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