In a WPF application I load news from an RSS feed. The contents are displayed in a TextBlock. This TextBlock has a certain size. Contents are cut off by the TextTrimming method.
Now I would like at the end of each TextBlock to insert a hyperlink button. The only problem is I do not know exactly what position will be cut on my string. Is there a way to figure this out?
When I insert my Text in my TextBlock and then my Hyperlink-Button, my HyperlinkButton will be cut of. Can I prevent to cut off my HyperlinkButton?
XAML-Code:
<TextBlock Name="myText" />
C#-Code:
Hyperlink hlink = new Hyperlink(new Run("here"));
myText.Inlines.Clear();
myText.Inlines.Add(value); //description from RSS Feed
myText.Inlines.Add(hlink);
Why not just add the HyperLink after the text, by replacing both items in a StackPanel?
If I understood your requirements this is one way to achieve your goals:
<StackPanel>
<DockPanel Width="200">
<TextBlock DockPanel.Dock="Left" Text="A short description." TextTrimming="CharacterEllipsis"/>
<TextBlock DockPanel.Dock="Right" TextAlignment="Right">
<Hyperlink NavigateUri="http://www.google.com">here</Hyperlink>
</TextBlock>
</DockPanel>
<DockPanel Width="200">
<TextBlock DockPanel.Dock="Left" TextTrimming="CharacterEllipsis" MaxWidth="170" Text="A really long descripion of the item." />
<TextBlock DockPanel.Dock="Right" TextAlignment="Right">
<Hyperlink NavigateUri="http://www.google.com">here</Hyperlink>
</TextBlock>
</DockPanel>
</StackPanel>
So the DockPanel control might be a good candidate to consider.
Slightly more convenient is to use a :
<TextBlock>
<Run Text="Short description"/>
<Hyperlink NavigateUri="http://www.google.com">here</Hyperlink>
</TextBlock>
Related
I've defined a for a listbox item, and binded the text inside the tooltip to two properties of the object (name + description) but i have an issue that the text is being cut off
here is my tooltip:
<ToolTipService.ToolTip>
<StackPanel >
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13">
<Bold>Name</Bold>
</TextBlock>
<TextBlock Text="{x:Bind name}"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13" TextWrapping="Wrap">
<Bold>Description</Bold>
</TextBlock>
<TextBlock Text="{x:Bind description}"/>
</StackPanel>
</StackPanel>
</ToolTipService.ToolTip>
Now the thing is, if i bind the tooltip to a method that returns the name + description (Which is how it was previously, but was super ugly) it does show all the text, it was like this:
<ToolTipService.ToolTip>
<TextBlock Text="{x:Bind Description}"/>
</ToolTipService.ToolTip>
But I needed to style it to make it look better, so i've tried to do what was posted above.
I've already tried setting the Width/Height to super large values, didn't do anything.
any ideas?
The tooltip template probably has a default maximum width, which cuts off the TextBlock. To solve this, just add TextWrapping attribute:
<TextBlock TextWrapping="Wrap" Text="{x:Bind description}"/>
Now the tooltip text will wrap on multiple lines as necessary
I have got multiple TextBlocks whose Text is inserted through DynamicResource. They are all set to TextWrapping="Wrap". But inside those Text-strings I have words which are not allowed to be split up. Those words must be kept as a whole word.
With hardcoded Text in Xaml it's quite easy solved via a TextBlock inside a Textblock:
<TextBlock TextWrapping="Wrap">
Example text with wrap and <TextBlock TextWrapping="NoWrap" Text=" example text without wrap"/
</TextBlock>
But this solution does not work when Text the is inserted through DynamicResource, because the text is not getting parsed.
How can I combine nowrap and wrap inside a DynamicResource Text without splitting it into multiple TextBlocks one after another?
PS: I have now created an example to demonstrate the behavior I would like (green) and the failed attempts (red, orange, darkred) of solving it:
<StackPanel HorizontalAlignment="Center" Width="80" Orientation="Vertical">
<TextBlock TextWrapping="Wrap" Foreground="green">
bla1 bla2 bla3 bla4 <TextBlock TextWrapping="NoWrap" Text="Together(en)"/> bla5 bla6 longWordWhichShouldBreak
</TextBlock>
<TextBlock TextWrapping="Wrap" Foreground="red">
bla1 bla2 bla3 bla4 Together(en) bla5 bla6 longWordWhichShouldBreak
</TextBlock>
<TextBlock TextWrapping="Wrap" Foreground="orange">
bla1 bla2 bla3 bla4 Together(en) bla5 bla6 longWordWhichShouldBreak
</TextBlock>
<TextBlock TextWrapping="WrapWithOverflow" Foreground="DarkRed">
bla1 bla2 bla3 bla4 Together(en) bla5 bla6 longWordWhichShouldBreak
</TextBlock>
</StackPanel>
Use NO-BREAK SPACE in your dynamic text. For example:
<TextBlock TextWrapping="Wrap">
Example text with wrap and example text without wrap
</TextBlock>
You can replace space with this char in those parts that you need this behaviour:
Replace(" ", System.Convert.ToChar(160))
Have you considered using 'WrapWithOverflow' instead of 'Wrap'?
This will only break the line if a space appears.
You can then set the words that must appear together with dashes,e.g.-
'regular line and words-that-shouldn't-break'
You should use Run:
<TextBlock>
<Run Text={x:static SomeText} />
<Run Text={x:static SomeNoWrapText}
TextWrapping="NoWrap"/>
<Run Text={x:static SomeMoreText} />
</TextBlock>
It's a bit much for a comment, but also not a complete answer.
Lets translate your example piece of XAML from all the implicit contents to a full qualified structure.
Your simplified XAML, using the implicit content properties and so on:
<TextBlock TextWrapping="Wrap" Foreground="green">
bla1 bla2 bla3 bla4 <TextBlock TextWrapping="NoWrap" Text="Together(en)"/> bla5 bla6 longWordWhichShouldBreak
</TextBlock>
Equivalent actual structure:
<TextBlock TextWrapping="Wrap" Foreground="green">
<TextBlock.Inlines>
<Run Text="bla1 bla2 bla3 bla4 "/>
<InlineUIContainer>
<InlineUIContainer.Child>
<TextBlock TextWrapping="NoWrap" Text="Together(en)"/>
</InlineUIContainer.Child>
</InlineUIContainer>
<Run Text=" bla5 bla6 longWordWhichShouldBreak"/>
</TextBlock.Inlines>
</TextBlock>
This should give you some idea about the complexity of what you have in your XAML. You can't archieve the same result by simply setting the Text property.
Currently I can't answer how to solve this issue, since DynamicResource is not enough information to start transforming into above structure.
You may want to have a look at this question: Data binding the TextBlock.Inlines
I'm trying to make a really simple title panel, which contains an Image-icon and TextBlock-title in the center of the panel:
To achieve this with TextBlock, I could use StackPanel with Vertical-orientation. But this doesn't work when I need to add the Image in front of TextBlock.
This is the current code, which doesn't work with the TextTrimming:
<StackPanel Orientation="Horizontal">
<Image
Height="28"
Width="28"
Margin="0,0,10,0"
Source="/Resources/Images/Icons/MyIcon.png"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
<TextBlock x:Name="TitleText"
Text="Test text test text Test asd asd "
VerticalAlignment="Center"
FontSize="28"
TextWrapping="NoWrap"
TextTrimming="WordEllipsis"/>
</StackPanel>
Another solution could be to control the Width of the TextBlock in code, but I'd like to achieve this within XAML. Any ideas?
I have the following XAML which displays correctly:
<GroupBox Name="RulesGroupBox" Header="Rules">
<StackPanel Name="RulesStackPanel"
HorizontalAlignment="Left">
....
</StackPanel>
</GroupBox>
I now want to make the header text bold using the following (which I know works in other projects):
<GroupBox Name="RulesGroupBox">
<GroupBox.Header>
<TextBlock FontWeight="Bold" Text="Rules"></TextBlock>
</GroupBox.Header>
<StackPanel Name="RulesStackPanel"
HorizontalAlignment="Left">
....
</StackPanel>
</GroupBox>
For some reason, in this project, this change has the effect of making the text displayed for the Header text "System.Windows.Controls.TextBlock" and not "Rules". The text is now bold but not displaying "Rules".
Any idea why the chagne would not show "Rules" in bold?
You likely have changed GroupBox's HeaderTemplate and this template supports displaying only text.
Header is defined more than once.
<GroupBox Name="RulesGroupBox">
<GroupBox.Header>
<TextBlock FontWeight="Bold" Text="Rules"></TextBlock>
</GroupBox.Header>
<StackPanel Name="RulesStackPanel"
HorizontalAlignment="Left">
....
</StackPanel>
</GroupBox>
"Rules" appears in bold with this correction.
Edit : This answer was made for the question before it has been edited. It's clearly not the good answer for the edited question.
How di I make a newline in the text for a checkbox? I've tried \n but it didn't work?
EDIT: this is my CheckBox
<CheckBox xml:space="preserve" Height="16" HorizontalAlignment="Left" Margin="360,46,0,0" Name="ShowOldRegistrations" VerticalAlignment="Top" Checked="ShowOldRegistrations_Checked" Unchecked="ShowOldRegistrations_UnChecked">
<StackPanel Height="42" Width="108">
<TextBlock>Line1</TextBlock>
<TextBlock>Line2</TextBlock>
</StackPanel>
</CheckBox>
<CheckBox Content="Stuff on line1
Stuff on line 2" />
You should not use a StackPanel for line-breaks, TextBlocks can do that easily:
<CheckBox>
<TextBlock>
<Run Text="Line 1"/>
<LineBreak/>
<Run Text="Line 2"/>
</TextBlock>
</CheckBox>
In WPF, you can put any control almost anywhere. So you could try this:
<CheckBox>
<StackPanel>
<TextBlock>foo</TextBlock>
<TextBlock>bar</TextBlock>
</StackPanel>
</CheckBox>
Also, you need to remove the Height property from your checkbox. Of course only one line gets displayed if the height doesn't permit displaying more.
In WPF, in most cases you don't need to (nor should) specify absolute dimensions for your controls. They can adjust automatically quite well.