WPF binding: Doing a temperature converter app - c#

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.

Related

How to display float values in SpinEdit?

I have a simple dialog with a SpinEdit control thot is supposed to work with integers and float values. It works correctly with integers, however it doesn't load float values. THe float value is loaded correctly to my Property but once the dialog opens it shows '0,00' instead of, say, '44,44'. If my float value has no decimal numbers it is also loaded correctly, so I can see values like '44,00'.
I tried casting to Decimal but to no avail. The only difference is that the value in my property has a dot '.' as a decimal placeholder while the SpinEdit shows a comma. From what I've read that shouldn't be a problem.
Here's my SpinEdit:
XAML:
<DataTemplate DataType="{x:Type local:TInputNumericVM}">
<dxe:SpinEdit x:Name="dxSpinEdit"
Text="{Binding Value, Mode=TwoWay}"
MaskType="Numeric"
IsFloatValue="{Binding FloatValue}"
MinValue="{Binding MinValue}"
MaxValue="{Binding MaxValue}"
Mask="{Binding Mask, Mode=TwoWay}"
MaxLength="{Binding Path=InputLength}"
MaskShowPlaceHolders="{Binding ShowPlaceHolder}"
InvalidValueBehavior="WaitForValidValue"
MaskUseAsDisplayFormat="True"
AllowRoundOutOfRangeValue="True"
Increment="{Binding IncrementStep}"
/>
</DataTemplate>
With 'IsFloatVakue = true', 'Mask = f', 'MaskShowPlaceholders = True'
EDIT:
I have managed to display the float value with a temporary workaround. I threw away the MaskUseAsDisplayFormat="True" property and used FormatDisplayString property where I set the value to "###.##". I'm not satisfied with this solution, but it works for now, the question is still opened and I'd appreciate further suggestions.
EDIT2:
I'm working on devexpress ver 16.1.6 and after consultations with my colleague he suggests that the MaskUseAsDisplayFormat property may work properly on a newr version but I can't use the newer version so I won't be checking it.
I am using the SpinEdit as well to display float/double values. Here is the code, which works for me:
<dxe:SpinEdit MinValue="1" Increment="0.5"
Mask="f1" MaskUseAsDisplayFormat="True" IsTextEditable="True"
EditValueType="{x:Type system:Double}"
EditValue="{Binding Path=XYZ, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
Guess the important properties to set are "Mask" and "EditValueType".
Using DevExpress 17.2.4.

Append extra text to a bound textblock in winrt universal application

<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}}" />

Bind method result to a Silverlight XAML TextBlock

I have a function which, given a string, returns the string translated to German, linked to the xaml I want to edit:
public string convert (string label) {
return Translator.translate (label);
}
This string is then used to label some parts of my interface with TextBlocks in Silverlight. This is one sample TextBlock of my code:
<TextBlock Text="Center" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" />
I would wish to modify the "Text" property to call convert passing the text "Center" as a parameter and show the return result as the Text. Is there any way to do that?
Greetings
You can make it a converter and apply it to the binding base in your TextBlock kind of like;
<TextBlock Text="{Binding Text,
Converter={StaticResource YourTranslatorConverterThingy}}"/>
Here's just one article with a bit more explanation. Hope this helps.

RadDateTimePicker + Mask for Data Entry

Requirement: Allow user to enter date in the format of ###### while also have the ability to choose it from a DatePicker (assuming all years entered will be Post 2000.) So for example, user enters 050513 it becomes 5/5/2013. Or if they choose it from the DatePicker, provide the same display. They also want it to select all when the control receives focus so they can easily overwrite any pre-existing date.
What I tried: My xaml is much stronger than my c# so I tried to get creative and got really darn close but no cigar, so hoping someone could fill in the blanks but here's where I got. I figured I could leverage what I know about RadMaskedTextBox and RadDateTimePicker so I intertwined their properties a bit (with the idea I could just use the Box for entry, and the picker for just the icon) but for testing purposes it was something like this;
<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Center" >
<telerik:RadMaskedTextBox x:Name="radMTB"
Value="{Binding SelectedDate, Mode=TwoWay,
ElementName=radDP}"
MaskType="DateTime"
Mask="d" SelectionOnFocus="SelectAll" />
<telerik:RadDateTimePicker x:Name="radDP"
InputMode="DatePicker"
SelectedDate="{Binding SelectedDate, Mode=TwoWay, ElementName=radMTB}"/>
</StackPanel>
This works great except for the fact that it still requires them to type the whole date with slashes. So I changed the MaskType to Standard with a Mask="##/##/\2\0##" to insert the "20" so they could enter like 121212 and it become 12/12/2012 right? Thought I had it until I tried to pass the value off to the DatePicker. I Bound the SelectedDate on the Picker to TextWithLiterals to get the display value, no luck. Tried Display.Text, Display.Value, just Text, just Value. It wouldn't work with any of them to bind too and pass off to the DatePicker to work...
So I guess my question is, can I still bind to that literal display somehow and make this work using only xaml? Or is ParseDateTimeValue code inevitably required for the RadDateTimePicker? If so, could someone please share some knowledge and help a guy out with it? Or is there another way all together to fulfill the sought after requirements? This cant be the first time this has come up?
Thanks for reading either way and cheers!
Instead of using RadMaskedTextBox, trying using Telerik's RadMaskedDateTimeInput. It is quite a bit more flexible with DateTime values.
<telerik:RadMaskedDateTimeInput x:Name="radMTB1"
IsClearButtonVisible="False"
FormatString="{}{0:M/d/yyyy}"
Mask="MMddyy"
SelectionOnFocus="SelectAll"
Value="{Binding SelectedDate, Mode=TwoWay, ElementName=radDP2}" />
More info: http://www.telerik.com/help/silverlight/radmaskedinput-features-controls-datetime.html

how to set number format for double type variable?

This might be a very simple question.
How do I make sure that double will have number style in following format?
03.00, 02.04
if i Set
double foo=1;
variable foo should have number format 01.00.
if i set
foo = 2.5
it should have value 02.50
Implementaion I have bound foo property to silverlight textbox through MVVM model.
I want to textbox to display number in 00.00 format always .
If you are using silverlight version 4.0 check this tutorial
Formatting made easy - Silverlight 4
You need to specify the string formate as shown in below example
<TextBlock Name="textBlock5"
Text="{Binding DoubleValue, StringFormat='0,0.00'}" />
Also check : Custom Numeric Format Strings on msdn
Simple format for formating such value is:
string val = 1d.ToString("00.00");
I don't know if silverlight textbox have special property for value formatting, probably you neeed to convert it before.
UPDATE
You can use binding described here: What is the WPF XAML Data Binding equivalent of String.Format?
set the StringFormat in your binding to "00.00" - if you show us your binding/xaml code I can give you more on that.
You can use StringFormat to format Bindings or use Converters
<TextBlock Text="{Binding DoubleValue, StringFormat=StringFormat=\{0:N2\}}" />
<TextBlock Text="{Binding DoubleValue,
Converter={StaticResource NumericFormatConverter},
ConverterParameter=N2}" />

Categories