New line in the text for a CheckBox using WPF? - c#

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.

Related

Long text in tooltip is not fully shown in UWP

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

c# WPF Combobox selected item text cut off

Not sure where in the combobox style I can fix this. If you need me to post code let me know, but the style code is pretty long. Below is the combobox less the style.
<ComboBox x:Name="user_combobox" Margin="115,62,0,0" Height="26" Width="306"
HorizontalAlignment="Left" VerticalAlignment="Top"
IsReadOnly="True"
Foreground="White" Background="SteelBlue" BorderBrush="White" OpacityMask="RoyalBlue"
Style="{StaticResource ComboBoxFlatStyle}"
ItemContainerStyle="{StaticResource ComboBoxItemFlatStyle}"
MaxDropDownHeight="{Binding User_Combobox_Height}"
ItemsSource="{Binding Username_List}">
<ComboBox.Resources>
<SolidColorBrush x:Key="ComboBoxHighlightBrush" Color="RoyalBlue" />
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=username}" Margin="0,-1,0,1" Height="22" FontSize="16" FontWeight="Bold" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
A wild guess, but is it possible that in your "ComboBoxFlatStyle" Style, you're setting a value for Template property of the ComboBox? If so, check your Margin value for any Control you've set there, you probably have some top or bottom margin that are too restrictive.
I know, this answer is pretty old, but in case anyone faces the same issue again:
I had the same exact problem. I solved it thanks to the answer of Roger Leblanc. Changing widths and heights didn't change anything. But adding a Padding of 0 (or higher) solved it.

TextBlock TextWrapping Wrap and NoWrap combined and Text through DynamicResource

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

RichTextBox with variable number of bound runs

I was working on a project recently where I feel a nice solution would be to display a paragraph in a RichTextBox that is bound to a collection of objects that will hold a string and a few properties about that string. I was hoping I could do something like this:
<RichTextBox Name="rtb" IsReadOnly="True" FontSize="14" FontFamily="Courier New">
<FlowDocument>
<Paragraph>
<Run Foreground="Red" Text="{Binding foo}"/>
<Run Foreground="Green" Text="{Binding bar}"/>
<Run Foreground="Blue" Text="{Binding baz}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
This works fine, however this is defining all the bindings ahead of time, but I actually won't know them until run time, each string will be stored into a collection. I was trying to think of a way to define and bind multiple Run blocks and place them all in the RichTextBox to be displayed. I would prefer a primarily xaml solution if possible, but if I must do it in the code behind that may be OK as well as long as the bindings stick and everything updates properly after the initial load.
I tried something like this, but was having trouble figuring how how to bind to the Text property of a Run from the code behind:
foreach (var item in col)
{
Run run = new Run();
Binding b = new Binding();
b.Source = this;
b.Path = new PropertyPath(item.StaticText);
run.SetBinding(run.Text, b); //Tells me Text is not a dependency property
//But it could be bound if this were xaml?
}
I also tried putting an ItemsControl inside the RichTextBox that had Run as the data template of the items, but it wouldn't allow me to make the data template a Run tag, and I'm not sure that would have worked anywway.
Any guidance on the best approach, or what's wrong with my solution.
You could use this...
<Paragraph>
<ItemsControl ItemsSource="{Binding MyRuns}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Foreground="{Binding Color}" Text="{Binding Text}"/>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate >
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<!--<Run Foreground="Red" Text="{Binding foo}"/>
<Run Foreground="Green" Text="{Binding bar}"/>
<Run Foreground="Blue" Text="{Binding baz}"/>-->
</Paragraph>
And define MyRuns as a collection on your view model, e.g.
public ObservableCollection<RunData> MyRuns { get; set; }
Put properties in your RunData etc.
And you can of course remove ItemsControl.ItemsPanel, that's just for fun
This seems to have gotten the job done for me, I created an ItemsControl that used a WrapPanel as the ItemsPanel. Then I can bind each item to my collection of strings and it comes out looking like a nice paragraph of variable strings. I'll keep the question open though in case someone comes up with a more elegant solution.
<RichTextBox Name="rtb" IsReadOnly="True">
<FlowDocument>
<Paragraph>
<ItemsControl ItemsSource="{Binding col}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock Text="{Binding StaticText,
UpdateSourceTrigger=PropertyChanged}"/><TextBlock Text=" "/>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Paragraph>
</FlowDocument>
</RichTextBox>

Insert Hyperlink in TextBlock

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>

Categories

Resources