我正在尝试创建自定义,以便在属性发生更改RoutedEvent
时触发动画。我的类继承自类,我隐藏了该属性。我正在使用 a来更改其他值的属性。我的代码不会产生任何错误,但它不会做任何事情。我确定问题出在事件上,因为当我用事件替换它时,一切正常。Text
TextBlock
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