Set size of InlineUIContainer? - silverlight

I wrapped a TextBlock in an InlineUIContainer inside a RichTextBox, its content is bound to an external multi-line TextBox that the user updates on the spot.
What happens is the InlineUIContainer's size doesn't expand or shrink as needed, it's a static size, I need it to be extended according to the size of the inner TextBlock (I've tested the size of the TextBlock and it does resize according to its content).
Any way to do this? Of course a XAMLy way is preferred, even more verbose.

I don't know if it's a Silverlight thing, but trying out this in Kaxaml works (typing in the Textbox expands it):
<RichTextBox IsDocumentEnabled="true">
<FlowDocument>
<Paragraph>
<Run>cool </Run>
<InlineUIContainer><TextBox>woohoo</TextBox></InlineUIContainer>
<Run>stuff</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>

I did an ugly and hacky workaround, hope to be able to replace it soon.
I made another property in the ViewModel, that returns:
string.IsNullOrWhitespace(comments) ?
string.Empty :
string.Format("Comments:{1}{0}{1}", comments, Environment.NewLine);
It works but is nasty.

Related

Hyperlink in WPF Richtextbox

I load WPF Richtextbox contents from Xaml string in which there are some Hyperlinks. When it is loaded into control, Hyperlinks are not clickable! I want to click on them and their associated URL shows up.
No freschx, it's about WPF. A WPF RichTextBox, unlike the one in WinForms, does not have a DetectUrls property. And it's weird you wrote a Xaml code for that, even weirder there is someone who thought it useful.
Check this post out where JHubbard80 and me had two different approaches to solve this problem.
To make the Hyperlink, or any inline UIElement in general, available for hit testing, we must set RichTextBox.IsDocumentEnabled to true.
To make the Hyperlink clickable without pressing the Ctrl key, the Hyperlink must be made read-only e.g., by wrapping it into a TextBlock or by making the complete RichTextBox read-only (by setting RichTextBox.IsReadOnly to false).
<RichTextBox IsDocumentEnabled="True">
<FlowDocument>
<Paragraph>
<Run Text="Some editable text" />
<TextBlock>
<Hyperlink NavigateUri="https://duckduckgo.com">
DuckDuckGo
</Hyperlink>
</TextBlock>
</Paragraph>
</FlowDocument>
</RichTextBox>
Ensure the DetectUrls property on the RichTextbox is set to true. You can then attach an event handler to the link clicked event and do what you wish.
<RichTextBox DetectUrls="True" />
Potential duplicate thread. Credit goes to Sam Meldrum from this thread.
For an even deeper analysis you may wish to try this article.

WPF: Custom window.title

I need to dynamically change the Windows.Title property. In short I need the application name to be left alligned and the filename to be centered all in the same line.
<Window.Title>
<TextBlock> test </TextBlock>
</Window.Title>
I tried out for starters with something like this, which apparantly is not allowed. Anyone has an idea?

WPF Styles Question

A simple question which I can't find the answer on the web for some reason...
I want to place the content to a ResourceDictionary:
<TextBlock
Style="{StaticResource HomePageTextStyle}">
<LineBreak/>
Hello<LineBreak/>
<Bold>World!</Bold>
<LineBreak/>
</TextBlock>
The best I could do was:
<s:String x:Key="HomePageTextContent">
Hello World!
</s:String>
Which stripped all the formatting from the content :( Help please~ Thanks in advance. Oh, and If you can recommend a nice reference for using WPF Styles, it would be great~ Thanks!
I'd say you want to use data-binding instead of applying a style, since you are putting content in the TextBlock not changing the appearance of the TextBlock itself, e.g. drawing a border around it.
According to MSDN: TextBlock supports the hosting and display of Inline flow content elements. To be more precise, the content of the TextBlock in your first code block becomes a InlineCollection in the Inlines property of the TextBlock. Unfortunately the Inlines property isn't a dependency property so we can't bind data to it. The Text property, on the other hand, is a dependency property but doesn't allow anything other than a String.
To make a long story short, I don't think you can achieve what you want using pure XAML.

Changing FontSize relationally to the windowsize with WPF?

Is it possible, that the FontSize getting smaller if I shrink the window and getting bigger if I enlarge the window?
Wrap your text inside a Viewbox.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Viewbox>
<TextBlock Text="Sizes to fit!"/>
</Viewbox>
</Window>
I've never tried this, but I imagine you can bind the font size property of your text to the window size through a converter method. I wouldn't try to bind directly, as that way madness lies.
The other option would be to handle the window resize events and send the font size to each control manually...
--edit--
Just searched Google and found this result, which may be what you want.

WPF: Use SpellCheck On Read-Only TextBox

I'm looking to display text with the wavey red lines where a word is misspelt, but I only want the text to be selectable, not editable. If I set the TextBox's IsReadOnly property to True or IsEnabled to False, the wavey red lines disappear and I can't get around it by putting something transparent as this will prevent the user being able to select sections of the text.
Is there anyway I can keep the red lines, allow the text to be selectable but prevent the actual text being modified?
Thanks
You could hook up to on the text change event of the text box, and just reject the new text. It would have the same effect of readonly without graying out the textbox or getting rid of the spell checking.
Thanks David. I'm currently looking at 2 possible solutions, yours and the following:
I've created a custom control which is based on the standard TextBox, but effectively has 2 textboxes laid on top of one another in this manor:
<TextBox Name="tbxBack"
Foreground="Transparent"
SpellCheck.IsEnabled="True"
TextWrapping="Wrap"
SnapsToDevicePixels="True"/>
<TextBox Name="tbxFront"
Background="Transparent"
TextWrapping="Wrap"
SnapsToDevicePixels="True"
IsReadOnly="True"/>
I think it's pretty straight forward what's going on here, but I'm concerned about the potential overhead this will cause.
The reason I'm looking into the double TextBox solution is that I'm worried if I try and cancel the event, it could end up with some sort of flashing in the control when the text is changed.

Resources