0

如何擴展現有控件(在我的情況下是ComboBox)以包含可以綁定到視圖模型上的屬性的新屬性?將自定義控件上的新屬性綁定到查看模型

我有扶養財產上的控制的類,如下所示:

public class MyComboBox : ComboBox 
{ 
    public static readonly DependencyProperty MyTextProperty = 
     DependencyProperty.Register("MyText", typeof(string), typeof(MyComboBox)); 

    public string MyText 
    { 
     get 
     { 
      return GetValue(MyComboBox.MyTextProperty).ToString(); 
     } 

     set 
     { 
      SetValue(MyComboBox.MyTextProperty, value);    
     } 
    } 

而且要聲明在XAML像這樣綁定到它:

<MyComboBox MyText="{Binding MyTextOnViewModel, 
    UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/> 

的結合是行不通的,任何想法爲什麼?

謝謝。

回答

2

當屬性聲明爲MyTextProperty時,您的getter和setter引用TestTextProperty。

你消氣也應鑄造的,而不是調用的ToString()

return (string)GetValue(MyTextProperty); 

請參閱本page的更完整的說明。

+0

你的第一個錯點,抱歉,生病檢查現在休息出 – andrej351 2009-07-22 04:58:03

相關問題