2013-05-09 97 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