2013-05-09 98 views
0

我試圖創建自定義RoutedEvent以便在TextBlockText屬性更改時觸發動畫。我的班級繼承了TextBlock班,並且我影響了Text特性。我正在使用Button以更改Text屬性的某些其他值。我的代碼不會產生任何錯誤,但它不會執行任何操作。我確信問題出在TextChanged事件上,因爲當我將其替換爲MouseEnter事件時,一切正常。我的自定義事件不會產生任何東西

Public Class MyCustomTextBlock 
Inherits TextBlock 

Public Shared ReadOnly TextChangedEvent As RoutedEvent = EventManager.RegisterRoutedEvent("TextChanged", _ 
       RoutingStrategy.Bubble, GetType(RoutedEventArgs), GetType(MyCustomTextBlock)) 

Public Custom Event TextChanged As RoutedEventHandler 
    AddHandler(ByVal value As RoutedEventHandler) 
     Me.AddHandler(TextChangedEvent, value) 
    End AddHandler 

    RemoveHandler(ByVal value As RoutedEventHandler) 
     Me.RemoveHandler(TextChangedEvent, value) 
    End RemoveHandler 

    RaiseEvent(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) 
     Me.RaiseEvent(e) 
    End RaiseEvent 
End Event 

Public Shared Shadows TextProperty As DependencyProperty = 
    DependencyProperty.Register("Text", GetType(String), GetType(MyCustomTextBlock), 
           New FrameworkPropertyMetadata(String.Empty, 
            New PropertyChangedCallback(AddressOf TextPropertyChanged))) 

Private Shared Sub TextPropertyChanged(ByVal sender As Object, ByVal e As DependencyPropertyChangedEventArgs) 
    DirectCast(sender, MyCustomTextBlock).RaiseEvent(New RoutedEventArgs(MyCustomTextBlock.TextChangedEvent)) 
End Sub 
End Class 

XAML

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

<Window.Resources> 
    <Style TargetType="local:MyCustomTextBlock"> 
     <Style.Triggers> 
      <EventTrigger RoutedEvent="local:MyCustomTextBlock.TextChanged"> 
       <EventTrigger.Actions> 
        <BeginStoryboard> 
         <Storyboard> 
          <DoubleAnimation Storyboard.TargetProperty="FontSize" To="30" Duration="0:0:1" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </EventTrigger.Actions> 
      </EventTrigger> 
     </Style.Triggers> 
    </Style>  
</Window.Resources> 

<Grid> 
    <local:MyCustomTextBlock x:Name="Textblock1" Text="xxxxxxxxx" Background="Yellow" Width="100" Height="25" /> 
    <Button Content="Change Text" Height="23" HorizontalAlignment="Left" Margin="217,218,0,0" Name="Button1" VerticalAlignment="Top" Width="75" /> 
</Grid> 

Class Main Window  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  
System.Windows.RoutedEventArgs) Handles Button1.Click 

    Textblock1.Text = "apollon" 
End Sub 
End Class 

回答

0

它看起來像問題是,你正在創建新的成員,與基地TextBox衝突。您應該考慮使用TextBox上已存在的TextChanged事件,或者如果您確實需要您自己的名稱,則可以將其命名爲不同的名稱。到目前爲止,你沒有做任何基礎屬性不能爲你做的事情。

您的具體問題的主要問題似乎與您創建的新的TextProperty相同。你不應該在基類中隱藏一個依賴項屬性,主要是因爲你無法控制在每種情況下使用哪一個屬性,而且還因爲內置了一些機制來使其不必要。在你的情況下,你通過沒有完全實現DP的樣板代碼而加重了這個問題。由於您沒有包裝屬性,因此您從代碼設置的Text屬性設置爲TextBox.Text,它在內部調用SetValueTextBox.TextProperty,因此完全跳過您的代碼。

而不是聲明自己的TextProperty你可以把你的元數據粘貼到現有的元數據上。要將更改處理程序添加到現有屬性,請將此調用添加到靜態構造函數中:

 TextProperty.OverrideMetadata(GetType(MyCustomTextBlock), 
          New FrameworkPropertyMetadata(String.Empty, 
          New PropertyChangedCallback(AddressOf TextPropertyChanged))) 
+0

感謝您的迴應!我使用文本塊而不是文本框...所以在基類中沒有TextChanged事件。 – apollon 2013-05-09 18:23:31

+0

所以我試過你的代碼,它工作正常。感謝很多優秀的職位! – apollon 2013-05-09 21:22:52