动态创建绑定并将其设置为Silverlight创建的字符串对象 [英] creating a binding dynamically and setting it to a string object that was created silverlight

查看:25
本文介绍了动态创建绑定并将其设置为Silverlight创建的字符串对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望动态创建绑定,并将此绑定设置为动态创建的字符串对象,并将其绑定到组合框的displayember路径属性。

如何着手进行此操作?

以下是我到目前为止编写的代码,但似乎不起作用。我将把绑定的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中不可用。

这篇关于动态创建绑定并将其设置为Silverlight创建的字符串对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆