question

NickLuo-2192 avatar image
0 Votes"
NickLuo-2192 asked Castorix31 answered

[WPF] Button following text in a TextBlock

I have a TextBlock which contains a button, and I expect that the button will follow the content text of TextBlock.
(The button should be attached to the end of the text of TextBlock.)

My working demo code:
131867-microsoftteams-image-1.png



Where "gdRoot" is an arbitrary Grid in XAML.
This code works fine until I change the content of the TextBlock.
Once I change the text of TextBlock, the button disappears.
In real case, the content of TextBlock is bound to a resource, which will change according to the language user selects.
And after "property change" event is triggered, or the text is re-assigned, the button disappears.
As I know, the button is added to the TextBlock's "Inlines" property, and the InlineCollection is intact after "property change" event.

Does anyone know how to avoid button from disappearing?

Thanks in advance.

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
1 Vote"
Castorix31 answered

You could do something like :

 TextBlock tb = new TextBlock();
 Run run1 = new Run("Text before the button... ");
 Button btn = new Button();
 btn.Content = "Button";
    
 tb.Inlines.Add(run1);
 tb.Inlines.Add(btn);
    
 gdRoot.Children.Add(tb);
 run1.Text = "New text";
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.