定制控制:当我的TextBlock内容更改时,如何更新文本块

发布于 2025-01-30 02:39:58 字数 1127 浏览 0 评论 0原文

如果这是一种USERCONTROL,则可以将其绑定为儿童的游戏,但这在用户控制中不会起作用。

我希望在文本框更改文本时进行文本块进行更新。我可以在启动时将其设置,但是之后不会更改。

protected override void OnApplyTemplate()
    {
        _MTBGrid = GetTemplateChild("MTBGrid") as Grid;
        _previewMarkdownBlock = GetTemplateChild("PreviewMarkdownBlock") as MarkdownTextBlock;
        _inputTextBox = GetTemplateChild("InputTextBox") as TextBox;
        _previewMarkdownBlock.Text = _inputTextBox.Text;

        base.OnApplyTemplate();
    }

    public string Text
    {
        get => (String)GetValue(TextProperty);
        set => SetValue(TextProperty, value);
    }

我尝试在XAML中添加一个文本变化的事件,但没有更新。我对此并不经验。

private void InputTB_TextChanged(object sender, TextChangedEventArgs e)
    {
        UpdateMarkdownBlock(this, _previewMarkdownBlock, _inputTextBox.Text);
    }
private static void UpdateMarkdownBlock(MarkdownTextBox markdownTBControl, MarkdownTextBlock markdownPreview, String newValue)
    {
        if (markdownPreview != null && newValue != null)
        {
            markdownPreview.Text = newValue;
        }
    }

If this was a usercontrol, it'd be child's play to bind it, but that won't work in a user control.

I want the TextBlock to update when the TextBox text changes. I can set it on launch, but it doesn't change afterwards.

protected override void OnApplyTemplate()
    {
        _MTBGrid = GetTemplateChild("MTBGrid") as Grid;
        _previewMarkdownBlock = GetTemplateChild("PreviewMarkdownBlock") as MarkdownTextBlock;
        _inputTextBox = GetTemplateChild("InputTextBox") as TextBox;
        _previewMarkdownBlock.Text = _inputTextBox.Text;

        base.OnApplyTemplate();
    }

    public string Text
    {
        get => (String)GetValue(TextProperty);
        set => SetValue(TextProperty, value);
    }

I have tried adding a TextChanged event to the xaml, but it doesn't update. I'm not very experienced with this.

private void InputTB_TextChanged(object sender, TextChangedEventArgs e)
    {
        UpdateMarkdownBlock(this, _previewMarkdownBlock, _inputTextBox.Text);
    }
private static void UpdateMarkdownBlock(MarkdownTextBox markdownTBControl, MarkdownTextBlock markdownPreview, String newValue)
    {
        if (markdownPreview != null && newValue != null)
        {
            markdownPreview.Text = newValue;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文