簡體   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