2014-02-18 60 views
1

我有一個定義了一些自定義依賴屬性爲TextBox類的類:如何使用自定義類中定義的依賴項屬性?

public class DependencyProperties:FrameworkElement 
{ 
    public static readonly DependencyProperty SelectionBeginProperty = DependencyProperty.Register("SelectionBegin", typeof(int), typeof(TextBox), 
                  new UIPropertyMetadata(0, SelectionStartDependencyPropertyChanged)); 
    public static readonly DependencyProperty SelectionLengthProperty = DependencyProperty.Register("SelectionLength", typeof(int), typeof(TextBox), 
                  new UIPropertyMetadata(0, SelectionLengthDependencyPropertyChanged)); 
    public static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register("Children", typeof (string), typeof (TreeView)); 

    static DependencyProperties() 
    { 

    } 
...  
} 

,當我嘗試在XAML中使用這些屬性:

<TextBox Name="TextBox_1735" 
     SelectionBegin="{Binding TextBox_1735SelectionBegin, UpdateSourceTrigger=PropertyChanged}" 
     SelectionLength="{Binding TextBox_1735SelectionLength, UpdateSourceTrigger=PropertyChanged}" /> 

它拋出一個異常,物業SelectionBegin能沒有解決。

+2

在你的情況下,你需要實現*附加*依賴屬性 - [''MSDN鏈接'](http://msdn.microsoft.com/en-us/library/ms749011(v = vs.110).aspx )。像這樣使用:''。 –

+0

我試過了。但現在我又遇到了另一個錯誤:「A'Binding'不能在'TextBox'類型的'SetSelectionBegin'屬性上設置。'綁定'只能在DependencyObject的DependencyProperty上設置。 – Sergiu

+0

@Sergiu在實現[自定義附加屬性](http://msdn.microsoft.com/en-us/library/ms749011(v = vs.110).aspx#custom)時,您在某處出錯了。 – franssu

回答

0

您應該尋找的是Attached Properties,因爲標準的依賴項屬性必須在控件本身中聲明。您也可以從TextBox繼承並在派生類中添加您的依賴項屬性。

0

我創建了一個簡單的類,應該看起來像一些評論。通過這個例子,你可以讓自己剩下的屬性。

AttachedProperty

public class DependencyProperties 
{ 
    #region Here put your property declaration 

    public static readonly DependencyProperty SelectionBeginProperty; 

    public static void SetSelectionBegin(DependencyObject DepObject, int value) 
    { 
     DepObject.SetValue(SelectionBeginProperty, value); 
    } 

    public static int GetSelectionBegin(DependencyObject DepObject) 
    { 
     return (int)DepObject.GetValue(SelectionBeginProperty); 
    } 

    #endregion 

    #region Here in constructor register you property 

    static DependencyProperties() 
    { 
     SelectionBeginProperty = DependencyProperty.RegisterAttached("SelectionBegin", // RegisterAttached 
                  typeof(int),    // Type of your property 
                  typeof(DependencyProperties), // Name of your class 
                  new UIPropertyMetadata(0, SelectionStartDependencyPropertyChanged)); 
    } 

    #endregion 

    private static void SelectionStartDependencyPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
    { 
     // Some logic 
     var textBox = sender as TextBox; 

     if (textBox == null) 
     { 
      return; 
     } 

     if (e.NewValue is int && ((int)e.NewValue) > 0) 
     { 
      textBox.Background = Brushes.Red; 
     } 
    } 
} 

XAML

<Window x:Class="AttachedPropertyHelp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:AttachedPropertyHelp" 
     Name="MyWindow" Title="MainWindow" 
     Height="350" Width="525"> 

    <Grid> 
     <TextBox Name="MyTextBox" 
       local:DependencyProperties.SelectionBegin="{Binding Path=Width, ElementName=MyWindow}" 
       Width="100" 
       Height="30" /> 
    </Grid> 
</Window> 

對於依賴屬性設置Window int類型的寬度,並且如果它是大於零,則TextBox標記紅色背景。

相關問題