0

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

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
4

1 回答 1

0

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

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

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

        TextProperty.OverrideMetadata(GetType(MyCustomTextBlock),
                             New FrameworkPropertyMetadata(String.Empty,
                             New PropertyChangedCallback(AddressOf TextPropertyChanged)))
于 2013-05-09T15:31:19.100 回答