定制控制:当我的TextBlock内容更改时,如何更新文本块
如果这是一种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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论