MaskedInput in Windows Phone - c#

I'm trying to implement MaskedInput TextBox at WP8. The common scenario: empty field, user see mask, e.g. ###-##-##. Then he starts to input values: 1##-##-##, 21#-##-##, 321-##-##, 432-1#-## and so on. I thought about using TextSelection property and TextChanged event to maintain right selection position and on-fly mask replacing with values. The problem is that I can't use different color for mask, as it share the same input with value. Also I can't use another TextBox or TextBlock behind active, because mask characters will have different width with value (e.g. ### will not align with W##). Is there some way to support multiple foreground at TextBox or to adjust characters width to same value?
UPD: Well, I know about monospacing fonts. Guess, it's the only way to get what I need. But is there some way to implement that feature in one TextBox?

Yes, You will Use like this.
<TextBlock>
<Run Text="*" Foreground="#FFE10101"/><Run Text="Required Line" />
<Run Text="Red" Foreground="Red"/>
<Run Text="Blue" Foreground="Blue"/>
<Run Text="{Binding yourString, StringFormat='with \{0\} a different color!'}" Foreground="Orange"/>
</TextBlock>
For More Reference Refer this Link

Related

How to allow HTML tags in WPF text block with binding?

I defined a TextBlock in xaml with binding:
<TextBlock Text="{Binding ElementName=MyClass, Path=MyStringProperty}"/>
When I set MyStringProperty to , for example , <b>Hello, World!</b> it shows it as plain text. Is there a way to tell the control to make the Hello, World! string bold?
You can't do this with a TextBlock like that.
You need to bind the FontWeight property of the text block to a variable that holds the bold/not bold value.
If the value is a boolean you need to write a converter to map the boolean value to the font weight property.
If you want to change the boldness (or any other property) of the text at runtime based on what the user types in then you need to be looking at using a RichTextBox.
Use the Inlines property:
<TextBlock Text="{Binding ElementName=MyClass, Path=MyStringProperty}">
<Run FontWeight="Bold" Text="{Binding BoldText}" />
</TextBlock>

WPF- Cut-off issue in WrapPanel

when i use more than one controls inside the wrap Panel if any control contains lengthy text (more than the window size) cut-off is happening. (See the image)
I have two textblocks and one button control.
<Grid>
<WrapPanel>
<TextBlock Text="Very long Text Message contains long text for testing " FontWeight="Bold"></TextBlock>
<Button Content="sample Text"></Button>
<TextBlock Text="sample Text textblock"></TextBlock>
</WrapPanel>
</Grid>
the cut-off happens for first text block. i want wrap to next line if the text contains more characters.
help me to solve the issue. thanks in advance.
You need the attribute TextWrapping="Wrap" in your textbox
<TextBlock
Text="Very long Text Message contains long text for testing "
FontWeight="Bold"
TextWrapping="Wrap">
</TextBlock>
Are you trying to make a single paragraph that wraps? If so, then WrapPanel isn't actually what you want.
WrapPanel takes UI elements (rectangular chunks of real estate) and lays them out left-to-right, top-to-bottom. You could enable wrapping in your first TextBlock, but then it'll take up a rectangle of screen space that's two lines high. Because the TextBlock fills that entire rectangle, the button will actually appear under it, rather than to the right of the bold words "for testing ".
If you want to make the whole thing flow like a paragraph, you don't want to use UI elements (which are always rectangular chunks); you want to use text elements (which flow in paragraphs).
The way to get text elements into your XAML is to wrap them in a TextBlock. Try this:
<Grid>
<TextBlock TextWrapping="Wrap">
<Bold>
<Run Text="Very long Text Message contains long text for testing " />
</Bold>
<Button Content="sample Text"></Button>
sample Text textblock
</TextBlock>
</Grid>
Note that I wrapped the first chunk of text in a <Run> element -- otherwise the trailing space would be ignored (assumed to be whitespace in your XAML document). The second chunk of text didn't have leading or trailing spaces, so I just put it directly inline.
This answer has a bit more about the difference between the "controls" and "text" sides of XAML.

Character encoding for use within ItemTemplate

I'm creating a menu from a ListBox. I'm using FontAwesome to create some font-icons. This is part of the ListBox ItemTemplate.
<TextBlock FontFamily="FontAwesome" VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="32" Text="{Binding MenuCode}"
ToolTip="{Binding Tooltip}" >
The problem resides within the TextBlock's Text. I need to display the symbol, not the menu code. So, for example, if I use Text="" directly, then the music icon appears (fixed for all the items), but when I use DataBinding (each item has a different symbol): Text="{Binding MenuCode}" then the text  (that is, the menu code as string) appears (as text, no icon). I guess problem is related with encoding, but can't fix it. Any idea?
Wrong escape sequence. HTML uses '#&x' while C# uses \u. So your "#&xF001" would become "\uF001"

How to change parts of the string in C#

I am writing a c# wpf application that get information from rss feed. I got a string with "<'b>Bold Text<'/b> and <'i>Italic Text<'/i> and <'u>Underline text<'/u>" and need to change the font styles to the correct style when I want to use the string for a label or textbox. The output now would be like Label: <'b>Text<'/b> instead of Text being BOLD. Need help to find a way to fix it.
Let's say you got the following text:
<b>Bold</b><i>Italic</i>
You need to break the texts up and put them into WPF tags. That text in WPF must look like this:
<TextBlock>
<Run Text="Bold" FontWeight="Bold" />
<Run Text="Italic" FontStyle="Italic" />
</TextBlock>
If you google HTML to XAML convertion, you should find some API's or examples you can use:
XAML to HTML Conversion Demo
HTML-XAML Converter

Remove the C# TextBlock inlines elements padding

About TextBlock UIElement in C#, we could add several Run object into it, which will be appended to inlines property. That's one way we show several pieces of text with different format(font, size and etc) in one TextBlock.
My question is: when I add, such as, two Run object into one TextBlock, there exist a padding between each Run object. For example, I add "12" and "34" Run objects, and finally they will be shown as "12 34" in the view. But what I need is they should be connected together as one word - "1234" - without that padding
Is there any setting we can use to prevent this padding?
Instead of
<TextBlock>
<Run Text="12"/>
<Run Text="34"/>
</TextBlock>
write the runs in one line like this
<TextBlock>
<Run Text="12"/><Run Text="34"/>
</TextBlock>
And the space will be gone.

Categories

Resources