2012-07-13 64 views
1

如何添加依賴項屬性到文本框並將依賴項屬性綁定到銀色光照下的布爾屬性。我的布爾屬性是在我的視圖模型中。將依賴項屬性添加到文本框

ImageSearchIsFocused是允許我在文本框中設置焦點的屬性。

<TextBox Text="{Binding ImgSearch, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
    <i:Interaction.Behaviors> 
     <common:FocusBehavior HasInitialFocus="True" IsFocused="{Binding ImageSearchIsFocused, Mode=TwoWay}" ></common:FocusBehavior> 
    </i:Interaction.Behaviors> 
</TextBox> 

ImageIsFocused物業

bool _ImageSearchIsFocused; 
public bool ImageSearchIsFocused 
{ 
    get { return _ImageSearchIsFocused; } 
    set 
    { 
     _ImageSearchIsFocused = value; 
     NotifyPropertyChanged("ImageSearchIsFocused"); 
    } 
} 
+0

什麼是你想添加的依賴屬性?您的示例僅顯示附加的行爲。你可以顯示你的代碼(例如行爲?) – 2012-07-13 08:11:42

+0

@HiTech魔術這是我遵循http://jklogic.blogspot.com/2012/02/silverlight-setting-focus-to-text-box.html – SupaOden 2012-07-13 09:08:17

+0

幫助。 ImageSearchIsFocused屬性的代碼是什麼樣的? – 2012-07-13 09:12:14

回答

0

如果你想添加一個依賴屬性,你將有子類中的文本框,並依賴屬性添加到您的子類。那麼你可以將它綁定到任何你喜歡的東西上:

public class MyTextBox : TextBox 
{ 

    public static readonly DependencyProperty MyBooleanValueProperty = DependencyProperty.Register(
     "MyBooleanValue", typeof(bool), typeof(MyTextBox), 
     new PropertyMetadata(new PropertyChangedCallback(MyBooleanValueChanged))); 
    public bool MyBooleanValue 
    { 
     get { return (bool)GetValue(MyBooleanValueProperty); } 
     set { SetValue(MyBooleanValueProperty, value); } 
    } 

    private static void MyBooleanValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var propValue = (bool)e.NewValue; 
     var control = d as MyTextBox; 

     // do something useful 
    } 

} 
+0

你不必爲它添加一個新的依賴項屬性就可以創建一個TextBox子類。只要typeof匹配,您可以將任何新的DependencyProperty添加到任何DependencyObject派生類。 – 2012-07-13 18:20:44