I want to display some value in a TextBox in Silverlight, but I need to set a unit value that remains constant.
Example:
100 gpm
I need to change the value (100), but the unit (gpm) should always stay static.
Here is my current Binding.
<TextBox Text="{Binding Path=Current.SystemDemand,Mode=TwoWay}" />
If you are using Binding on the TextBox You can set the Format of the Binding.
Here is how you set the format in Xaml
<TextBox x:Name="myTextBox" Text="{Binding Path=Current.SystemDemand, Mode=TwoWay, StringFormat='\{0} gpm'}" />
What the question author's approach and the accepted answer have in common is that they mix up the (editable) value and the (non-editable) unit.
Why not place an ordinary TextBlock containing the unit near the end of your TextBox?
You can even use a Grid to have the unit appear "inside" the TextBox.
<Grid>
<TextBox Text="{Binding...StringToNumberConverter...etc}" Padding="0,0,24,0"/>
<TextBlock Text="gpm" HorizontalAlignment="Right" IsHitTestVisible="false"/>
</Grid>
Related
I am pulling some data from a remote DB and inserting them into a ResourceDictionary. My app will take the a key and get a value based on the chosen language.
My XAML looks like this:
<Label Text="{Binding Dictionary[, Something:]}"/>
<Label Text="{Binding Dictionary[" Why" - where what!]}"/>
So, if a user chooses Danish, the Label will use the key ", Something" and translate it to the Danish word.
The problem I'm having seems to be that I can't use special characters like "," and ":".
I have tried replacing those with Unicode and ASCII characters but it didn't work.
<TextBlock>
<Run Text="{Binding Percentage}"/>
<Run Text="%"/>
</TextBlock>
I know this seems like a trivial question but I was wondering in the above code if Percentage value was 95 then it displays 95 % and not 95%. That is there is a space between the Run which I don't want. How to remove this.
Thanks in advance.
Try writing them in the same line. e.g.
<TextBlock>
<Run Text="{Binding Percentage}"/><Run Text="%"/>
</TextBlock>
Some days back i saw an example of what i need now but couldn't remind what was the way.
I want to show the text description in TextBlock and if the text is more that the size of text block, show the ...
Use below code to achieve this,
<TextBlock Text="{StaticResource someText}"
TextWrapping="Wrap" TextTrimming="CharacterEllipsis"
Margin="10"/>
And possible values of TextTrimming are as below,
None – no ellipsis, text is clipped (the default)
CharacterEllipsis – display as many characters as possible, followed
by an ellipsis
WordEllipsis – display as many words as possible, followed by an
ellipsis
I know link only answer are frowned upon but
TextBlock.TextTrimming Property
<TextBlock
Name="myTextBlock"
Margin="20" Background="LightGoldenrodYellow"
TextTrimming="WordEllipsis" TextWrapping="NoWrap"
FontSize="14">
One<LineBreak/>
two two<LineBreak/>
Three Three Three<LineBreak/>
four four four four<LineBreak/>
Five Five Five Five Five<LineBreak/>
six six six six six six<LineBreak/>
Seven Seven Seven Seven Seven Seven Seven
</TextBlock>
I have a grid with 3 columns:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
I want to have the last column align on the right, near to edge of screen.
Currently, the third column occurs soon after the second. I don't want to pass width in pixels, rather automatic calculation.
I have tried to use on the element in third column property HorizontalAlignment="Right", but unfortunately it's not working.
If your Grid is inside of a Stackpanel , no matter what you do it'll only resize iteself to the width of Stackpanel.
If your last column definitions is Auto , you can't align it to the right of the screen.Change 2nd definition to Auto and the last one to *.
Since your last column has Auto width, setting HorizontalAlignement of the content to Right should not be necessary. My guess would be either
your Grid doesn't have a HorizontalAlignement value of Stretch (which should be default), or
whatever contains the Grid (a StackPanel maybe?) is not giving it the full width.
I could be sure if I could check the rest of the XAML. Check these two things first. If these don't help, please post the rest of your XAML.
If you have the same type of controls inside the grid, you can assign a style for all of them in <Grid.Resources>, with setting the HorizontalAlignment property only if the Grid.Column property's value is 2 (with triggers).
But it is even easier for looping through all the children, and checking the column property of each and then setting the alignment, like
foreach (Control ctrl in yourGrid.Children) {
if(Grid.GetColumn(ctrl) == 2) ctrl.HorizontalAlignment = HorizontalAlignment.Right;
}
Is it possible to add dynamic colors to a TextBlock ..i.e. have one character in one color and the next in another color.
<TextBlock Text="{Binding no}" TextWrapping="Wrap" Margin="10,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}" FontSize="40" Foreground="#A400C4FF" >
// Can we add something here to specify what colours for what chars
</TextBlock>
Basically I input a dynamic 4 character sequence from no. I've bound it to this TextBlock inside a ListBox. Is it possible to have the characters in different colors.
If so is it possible to add these colors dynamically for eg. If I click a button certain characters change color?
Thank You. Any Help is appreciated.
Actually, you can, which can come in handy when you're doing a StringFormat on a data bound Textblock or a number of other places.
If you did want to try it though, like here's an SL example for a form label that puts a red asterisk next to the text Required Fields, but then can also add more stuff to it as shown in the example. Should work for Silverlight, WPF, UWP, etc...
<TextBlock>
<Run Text="*" Foreground="#FFE10101"/><Run Text="Required Line" />
<Run Text="Red" Foreground="Red"/>
<Run Text="Blue" Foreground="Blue"/>
<Run Text="{Binding SomeString, StringFormat='Hell ya you can make \{0\} a different color!'}" Foreground="Orange"/>
</TextBlock>
I'm developing for Mango with the WP7 SDK. You can use a <Run>. It seems a little buggy on WP7 , you have to add a spaces on the Run.Text property to get the spacing correct:
<TextBlock>Hello<Run Foreground="Bisque" Text=" Holla "></Run>and hello again!</TextBlock>;
to set foreground color dynamically to a textblock
use: txtblockname.Foreground= new SolidColorBrush(Colors.Yellow);
The TextBlock does not support multiple Foreground colors.
You could recreate this behaviour by using multiple textblocks (one for each letter) and placing them within a wrappanel. You could then change the color of individual characters/letters as you wish.
Beware of the probable performance impact this may have. The margins around individual letters will need to be adjusted to recreate standard behaviour though. Be especially careful around punctuation.