WPF Label Flowdirection text does not reverse with special characters - c#

I want to display the text "1/min" in a label with reversed FlowDirection, i.e. RightToLeft. However, the text remains the same with either "1/min" or "min/1".
Test code:
<Grid>
<StackPanel>
<Label FlowDirection="RightToLeft">1/min</Label>
<Label FlowDirection="RightToLeft">min/1</Label>
</StackPanel>
</Grid>
Both labels show the same text, namely "min/1". I'm fairly certain that non-ascii chars cause this, but how do I get around that behaviour?

Related

C# WPF how to set tooltip size in a tabcontrol

I have a tabcontrol in which each tabITem is rendered a very big font symbol (see image below)
now the tooltip is set to a very big size according to the very big size of the aforementioned tabItem char.
What I'd like is to keep the tabItem symbol very big but being able to resize the tooltip font.
PLUS
I can't understand why the tabheader tooltip is randomly set on all children. I want it on the tabheader itself and not floating everywhere!
--EDIT---
As requested here is a relevant part of my xaml. I only include tab2 which is shorter but there are several tabItems and all behave the same way.
<!-- +++++++++++++ TAB2 ++++++++++++ -->
<TabItem Name="tabItem2" HorizontalAlignment="Center" Height="80" IsSelected="false" FontSize="{StaticResource TAB_FONTSIZE}">
<TabItem.Header>
<StackPanel>
<TextBlock Text=""/> <-------------unicode symbol very big!
<TextBlock Name="tbTab2" Visibility="Hidden" FontSize="{StaticResource BUTTON_FONTSIZE}" />
</StackPanel>
</TabItem.Header>
<TabItem.Background>
<ImageBrush/>
</TabItem.Background>
</TabItem>
while tooltip is set in code-behind
tabItem2.ToolTip = Langs.Word(Langs.eWords.Pallet);
and it gets the HUGE size of the aforementioned unicode char.
Thanx for any help
The problem here is that you set both font size and tooltip directly on the TabItem, and those are then "inherited" by the item's header and content (the font size is also "inherited" by their tooltips). So in order to accomplish what you're asking you need to be more precise when setting those properties, i.e. set them on appropriate controls and not on the TabItem itself. In your case the following should work as expected.
You want to use the large font only for the unicode character, so set the FontSize property only on the control displaying that character:
<TextBlock Text="" FontSize="{StaticResource TAB_FONTSIZE}" />
You want the tooltip to be displayed only for the header, therefore you should set it on the header. You can do that either in XAML:
<TabItem.Header>
<StackPanel ToolTip="My tooltip text">
...
</StackPanel>
</TabItem.Header>
or in code-behind:
var stackPanel = (StackPanel)tabItem2.Header;
stackPanel.ToolTip = "My tooltip text";

How to display text in a WPF GridView that contains carriage returns

I have a grid view that has two simple columns, a report number and a short description. It is common for this short description to have some carriage returns. If that is the case, I would like the row to expand its height to fit all of the text in.
Currently the field renders with possibly the first line displaying, but then cutting off text below it. How could I display all the text, with it adjusting the height as needed?
And can this be done on a per-row basis, as in, if the next report did not have a description with carriage returns, it would just show on one line?
It would be rather similar to this question, but in WPF versus ASP.net
This also may due to using a theme application wide. If that is the case, how can I enforce it only in this grid view?
XAML:
Originally I had it only as
<GridViewColumn Width="365" Header="Desc" DisplayMemberBinding="{Binding desc}" />
I have also tried
<GridViewColumn x:Name="colDesc" Header="Desc" Width="75" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Desc}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
We are using an application wide theme, that very well may be setting the high explicitly. If so, I am leaning mostly towards the fact (From the comments) that the theme is overriding a default value for how this works. If so, how do I have it act the way the it is supposed to (again, from the comments, it sounds like this is the default behavior).
Using the second example, here is what it looks like:
In here it looks like the text is larger, so again, I would want the cell to adjust to the hight needed. And some of these have multiple lines, so as you can see I would not see the remaining content. I have also tried to set the Height on the text block, but that had no effect.
This worked.
public string TextBlockText { get { return "one line \r\ntwo line"; } }
<TextBlock Height="Auto" Width="100" HorizontalAlignment="Left" Text="{Binding Path=TextBlockText}" />
I will leave it to you on how to put a TextBlock in a ListView.

Synchronize generated items from ItemsSource

I'm looking for an easy way to synchronize Text sizes (Button Content), which are generated by an ItemsControl.
I'm using the following Xaml code:
<ItemsControl ItemsSource="{Binding UseCases}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding DisplayName}" Width="200" Height="200">
<Button.ContentTemplate>
<DataTemplate>
<Viewbox>
<ContentPresenter Content="{Binding}" />
</Viewbox>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I want to make the text on the buttons as big as possible, which already works.
But since the text length is different, the text sizes for each button is also different, which looks odd.
Is there a simple way to tell the Viewbox (or any other way) to take the size of the smallest text and use it for every button?
to limit the size of text just use Padding:
Handling text overflow is the complex part. By some reason (the culprit is - ButtonChrome) the button won't take 'TextBlock.TextTrimmingProperty' attached property, however the AttachedProperty mechanism is designed specifically for the cases like that), leaving you with two options:
Override button's template, lookup for ButtonChrome, get rid of it and replace with something, which has a TextBlock, bind that TextBlock's text to ContentControl.Content.
Manage your text overflow by yourself. Sibscribe for SizeChanged event, get size from the event argument (it mightn't be available anywhere else), get the padding and figure out if text exceeds the available size. Replace the excessive part of it with "..".
The moral - not worth doing.
I'd create uniform quadratic launch buttons and put labels beside them.

List box in c# windows phone 7

Does list box allow placing a paragraph of word into it instead of a item one after another?
I think you just need to put a text block inside a scroll viewer, but this article shows how to make a control:
See this article
This is some code that does the basic work:
<ScrollViewer x:Name="MyScroller">
<TextBlock x:Name="MyTextBlock" TextWrapping="Wrap" Text="Some very long text here..." />
</ScrollViewer>
You'll see that if the area containing the text is bigger than the size of the textblock, you'll be able to scroll to view all text.

How to create multiline textbox which shows apostrophe before each line

I want a multiline textbox which shows apostrophes before and after each line.
So that the textbox looks like:
" Hello this "
" is a funny "
" string test "
Or for example:
// This is
// a muliline
// comment.
Edit: These special characters must be readonly and if the user copies text from the textbox these characters should not be included.
This is very easy using two textboxes laid directly over one another. The rear one is a normal textbox with extra padding and transparent text. The front one has your extra characters but has its borders hidden and is IsHitTestVisible=False and Focusable=False so it doesn't interact with the user. The user interacts exclusively with the rear textbox but the front textbox is the one that displays the text. A binding with a value converter keeps the front textbox displaying exactly what the rear textbox displays, plus the extra characters.
This is how it would look:
<ControlTemplate x:Key="TextBoxWithExtraCharacters" TargetType="{x:Type TextBox}">
<ControlTemplate.Resources>
<!-- Remove the border from the inner textboxes -->
<ControlTemplate TargetType="{x:Type TextBox}">
<Decorator x:Name="PART_Content" />
</ControlTemplate>
</ControlTemplate.Resources>
<!-- Now add our own border -->
<Border
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<!-- Scrolling must happen at this level so both text boxes scroll simultaneously -->
<ScrollViewer>
<Grid>
<!-- Rear textbox provides editing and user interaction but the text is transparent -->
<TextBox
Margin="10,0,10,0"
Foreground="Transparent"
Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}"
Background="{x:Null}"
IsReadOnly="{TemplateBinding IsReadOnly}"
IsUndoEnabled="{TemplateBinding IsUndoEnabled}"
AcceptsReturn="{TemplateBinding AcceptsReturn}"
AcceptsTab="{TemplateBinding AcceptsTab}"
/>
<!-- Front textbox displays modified text but does not interact with user -->
<TextBox
IsHitTestVisible="false"
Focusable="false"
Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent},
Converter={x:Static ExtraCharacterConverter.Instance}"
Background="{x:Null}"
IsReadOnly="{TemplateBinding IsReadOnly}"
IsUndoEnabled="{TemplateBinding IsUndoEnabled}"
AcceptsReturn="{TemplateBinding AcceptsReturn}"
AcceptsTab="{TemplateBinding AcceptsTab}"
/>
</Grid>
</ScrollViewer>
</Border>
</ControlTemplate>
ExtraCharacterConverter is a simple IValueConverter class that implements the Convert method by taking the given string, appending quotes or // or whatever to it, and returning the result.
Note that I hard-coded a left and right margin of 10 units on the rear textbox, which assumes a particular width for the quote characters. This should be exactly the width of the added characters to make the text line up correctly. You want to get this right, or your caret and text selection positioning will be wrong. Also note that the correct value will change as you vary your font size and your choice of extra characters.
An easy alternative to hard-coding the margin would be to set it to a multi-binding on FontSize, FontFamily, FontWeight, etc, then use a IMultiValueConverter to compute the proper margin given this these values.
Note: This solution is slighly unsatisfactory when it comes to the color scheme for text selection. This can be fixed, but it requires a more complex solution: The rear text box is the same but its text is not invisible. The front text box is replaced with a RichTextBox (or TextBlock) whose content is computed dynamically to be the text with extra characters, but the regular text transparent. Because it is a RichTextBox the extra characters can be visible while the others are transparent.
Can you not simply create a user defined textbox using inheritance with the multiline property set to true, and some special code on the text changed event to check the first and last index on each line to ensure it has an apostrophe there? or if there is no way to iterate lines, can you just watch the character before and after every chr(11) or chr(13)?
This wouldn't be difficult with winforms, i'm not sure about WPF however.
~~~~~~~~~~~~~~~~~~~~~~~~~Edit 1: 11/25/2009 9:12 AM CT~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you would like it so that the user cannot edit or mess with the quotes, then you could write specific code in the KeyPressed event handler (or override the OnKeyPressed function if you are doing an inherited control) to keep them from adding characters before or after a chr(11) or chr(13), and add it back immediately if they attempt to delete it, and cancel invalid keystrokes.
I've written code similar to this in VB .NET for a money text box field. It may help you to understand what i'm talking about, and it may assist you in what you are doing:
Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
If Me.SelectionStart = 0 Then
Me.SelectionStart = Me.Text.Length
End If
End Sub
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
If Not Me.Text.IndexOf("$") = 0 Then
Me.Text = "$" + Me.Text.Replace("$", "")
End If
If Me.SelectionStart = 0 Then
Me.SelectionStart = Me.Text.Length
End If
End Sub
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If NOT ((Char.IsDigit(e.KeyChar) Or (e.KeyChar = CChar(".") And Not Me.Text.Contains(".")) Or Char.IsControl(e.KeyChar)) And (Not Char.IsDigit(e.KeyChar) Or (Me.SelectionStart <= Me.Text.IndexOf(".") + 2 Or Me.Text.IndexOf(".") = -1))) Then
e.Handled = True
End If
End Sub
I'd say your best bet is to implement your own textbox (inherit from textbox itself as a base, so you get all of the functionality for free) and override the drawing code and writing out each line yourself (pre/post-pending the apostrophes as you write out each line).

Categories