我的自定义事件不会产生任何结果

时间:2013-05-09 13:17:03

标签: wpf vb.net routed-events

我正在尝试创建自定义RoutedEvent,以便在Text的{​​{1}}属性发生更改时触发动画。我的类继承自TextBlock类,我隐藏了TextBlock属性。我正在使用Text来更改其他值中的Button属性。我的代码不会产生任何错误,但它不会做任何事情。我确定问题出在Text事件上,因为当我用let {s} TextChanged事件替换它时,一切正常。

MouseEnter

XAML

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

<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>

1 个答案:

答案 0 :(得分:0)

看起来问题是您创建的新成员与基础TextBox上的成员冲突。您应该考虑使用TextChanged上已存在的TextBox事件,或者如果您确实需要自己的事件,请将其命名为不同的事件。到目前为止,您还没有做任何基本属性不适合您的事情。

您的具体问题的主要问题似乎与您创建的新TextProperty有关。您不应该在基类上隐藏依赖项属性,主要是因为您无法控制将在每种情况下使用哪个属性,还因为内置了一些机制使其不必要。在您的情况下,您通过不完全实现DP的样板代码来加剧问题。由于您没有包装器属性,因此您在代码中设置Text属性的设置为TextBox.Text,内部调用SetValue TextBox.TextProperty,因此完全跳过您的代码。

您可以将元数据添加到现有元数据中,而不是声明自己的TextProperty。要向现有属性添加更改处理程序,请将此调用添加到静态构造函数中:

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