Append extra text to a bound textblock in winrt universal application - c#

<TextBlock Text="{Binding Amount}" />
I have this text block, it simply shows a number what ever I bind.
What I want is that it could append 'AED' before the bindings. Mean if 4.00 is bound, it would show this:
AED 4.00
If I don't want to create another textblock and use it for AED and don't want to change the value of binding to 'AED 4.00' from '4.00', then is there a way to do this kind of thing ?

You should implement IValueConverter and use it in your XAML for converting the double value to string.Format("AED {0}", value);
Take a look at this sample

For WPF applications use this:
Simply put a StringFormat to the Binding
<TextBlock Text="{Binding Amount, StringFormat=AED {0}}" />

Related

Bold Matched search string in a AutoSuggestBox?

I'm a little surprised to see that this isn't a feature of the AutoSuggestBox but, I'm trying to configure an AutoSuggestBox to bold any matched text from what gets pulled up.
So it would look like:
[ jay ]
jaydeflix
jaydeflixutil
tommyjay
If anyone has sample code, I'll gladly look at it, but I'll even gladly take a pointer at what to dig into (I'm self-taught, so I'm used to digging, just my binggle-fu is coming up short on this one).
The suggestion list is actually a ListView of AutoSuggestBox. More details please see AutoSuggestBox styles and templates. To define a custom look for each item in the list, use the ItemTemplate property of AutoSuggestBox, for more details about this please check the Text changed section.
The TextBlock can be set with paticial bold text with <Run> tag that you can try to use inside the template. For example:
<AutoSuggestBox
x:Name="asb"
... >
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run FontWeight="Bold" Text="{Binding QueryString}"></Run>
<Run Text="{Binding DisplaySpare}"></Run>
</TextBlock>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>
For more about how to contact the string to display, set the display order and bind to ItemsSource please try it yourself. This depends on the original source and your special requirements.

C# WPF textbox when binding data,how to add other strings in the Text property?

<TextBox x:Name="V2" Grid.Row="2" Grid.Column="8" Grid.ColumnSpan="3" Text="{Binding airPlane}".../>
In the above code, if I want to add more things in the "Text" property besides binding the airplane data, e.g. to add a unit called "Pounds", how to modify the code? Thanks!
You can use Converters for it. For more see WPF Converters

List<String> Databind to multiple Textbox on WPF

Hi I have read some tutorials on Data Binding but I can't seem to find what I need which is a DataBinding of a List to a Multiple TextBox.
I'm not sure if this is possible on WPF and I'm quite new to Data Binding so bear with me.
I have a List(Global Variable)
and let say it has 3 words which are "Apple", "Banana" and "Orange"
this 3 words are given by the Code Behind of my XAML.
In my XAML I have 3 TextBox, from first to third I want to assign my List to it so from the
first textbox -> Apple
second textbox -> Banana
third textbox -> Orange
Now the catch here is that I need it to work in 2 ways which means for example
I edit the first textbox to "Mango", the List on the Code Behind will also change.
Is this possible on Data Binding?
You can just bind to the element of the list.
<TextBox Text="{Binding Path=FruitList[0]}" />
<TextBox Text="{Binding Path=FruitList[1]}" />
<TextBox Text="{Binding Path=FruitList[2]}" />
Edit:
The default binding method of a TextBox is TwoWay, but you can add this if you want to make it clear.
<TextBox Text="{Binding Path=FruitList[0]} Mode=TwoWay" />
You can also add UpdateSourceTrigger=PropertyChanged to make the List update on every key stroke.
Edit2:
To acces the objects gcreated in code behind you have to set tht DataContext of your WPF. See wpf xaml binding to object created in code behind

Selecting Text inside a Multi-Inline TextBlock

I have some textblocks composed of Runs. I want the characters within the textblock to be individually selectable like one would expect.
Example XAML:
<TextBlock>
<Run Text="Cant-individually-select" />
<Run Text="Still-Cant-individually-select" />
</TextBlock>
Is there any way to make the run inside the textblock behave like a standalone run? I see there a lot of solutions involving styling a TextBox to look like a TextBlock, but this isn't possible when I need multiple runs.

WPF binding: Doing a temperature converter app

I'm doing a little app that basically has 2 text boxes. You'll enter Fahrenheit into TextBoxA and Celsius into TextBoxB.
As the text changes in TextBoxA I want the equivalent Celsius value to be displayed in TextBoxB and vice versa.
I can come up with a solution pretty easy for this but i'm trying to be a little clever.
Is there a way to do it all in Xaml except for a Convert class that does the maths? So basically I want the TextChanged event of one textBox to pass in it's value into a Converter class that is evaluated and sent to the other TextBox and visa versa.
Anyone know how I can achieve this ... and if it's possible at all?
<TextBox x:Name="Celsius" />
<Textbox x:Name="Fahrenheit" Text="{Binding Text, ElementName=Celsius, Mode=TwoWay, Converter={StaticResource CelsiusToFahrenheitConverter}}" />
When you update C, F will get the value and convert it. When you update F, it will convert back to C and push the value to the Celcius textbox.
<TextBox x:Name="CelcuiusTextBox" Text="{Binding Text, ElementName=FahrenheitTextBox, Converter={StaticResource FahrenheitToCelciusConverter}}"/>
<TextBox x:Name="FahrenheitTextBox" Text="{Binding Text, ElementName=CelciusTextBox, Converter={StaticResource CelciusToFahrenheitConverter}}"/>
Assumes the existence of two distinct converters. Of course, you could write a single converter that can be put in a different mode to convert either from or to celcius.

Categories