繁体   English   中英

绑定使用“新”文本属性的自定义 TextBlock 的文本属性

[英]Binding the Text Property of a custom TextBlock which uses “new” Text Property

我有一个自定义的TextBlock ,它用new覆盖了基本的Text属性:

public new string Text
    {
        set
        {
            if (value == null) return;
            if (value.Equals(base.Text)) return;

            if (string.IsNullOrEmpty(value))
            {
                // Hide text
                SlideOut();
                FadeOut((_, _) =>
                    base.Text = value);
            }
            else if (!string.IsNullOrEmpty(base.Text))
            {
                // Hide, then show text
                SlideOut();
                FadeOut((_, _) =>
                {
                    base.Text = value;
                    SlideIn();
                    FadeIn();
                });
            }
            else
            {
                // Show text
                base.Text = value;
                SlideIn();
                FadeIn();
            }
        }
    }

我是 Binding 的忠实粉丝,所以我试图这样使用它:

<customElements:AnimatedTextBlock
        x:Name="WarningTextBlock"
        Text="{Binding Warning}"
        FontWeight="Bold"
        Margin="4,8"
        TextWrapping="Wrap" />

OnPropertyChanged(); 更新TextBlock的属性,但跳过我的new Text属性。 如果我改回TextBlock ,一切正常。

我可以手动应用WarningTextBlock.Text = "WARNING TEXT"; 并且它有效,但我想了解是否可以将我的new属性而不是带有绑定的基本TextBlock.Text

在对我应该研究DependencyProperty知识进行了一些研究之后,我想出了如何实现它。

这是最有用的代码:

    public static readonly DependencyProperty TextContentProperty = DependencyProperty.Register(
        "TextContent", // Name of your actual Property
        typeof(string), // The type of your Property
        typeof(AnimatedTextBlock), // The type of the parent element
        new FrameworkPropertyMetadata(OnTextChanged)); // The method which is called after the base Property changes

    public string TextContent
    {
        // These are the first to get called
        get => (string) GetValue(TextContentProperty);
        set => SetValue(TextContentProperty, value);
    }

    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var tb = (TextBlock) d;
        var newText = (string) e.NewValue;

        // Do things with your element
        // Such as: tb.Text = newText;
        // In my case, do animations            
    }

可能没有必要申报新的财产。

您可以为现有属性添加另一个 PropertyChangedCallback,如下所示:

public class AnimatedTextBlock : TextBlock
{
    static AnimatedTextBlock()
    {
        TextProperty.AddOwner(
            typeof(AnimatedTextBlock),
            new FrameworkPropertyMetadata(OnTextChanged));
    }

    private static void OnTextChanged(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBlock = (AnimatedTextBlock)d;
        var text = (string)e.NewValue;

        // do something here
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM