How to display float values in SpinEdit? - c#

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.

Related

How to fix VerticalLineAnnotation always setting binding to zero

I'm trying to set up a SciChart VerticalLineAnnotation similar to the Drag Horizontal Threshold example in the SciChart example suite. I have the annotation bound to a property in my view model, but when dragging the annotation it continually sets the property to a value of 0.
I've tried setting the property value in code and it does move the line as you would expect indicating the binding is fine. I set a breakpoint on the property and confirmed the 0 value is coming from the control, not my code.
<sc:SciChartSurface x:Name="OverviewSurface"
Margin="5,2,5,2"
Background="White"
Grid.Row="2"
Grid.ColumnSpan="4"
Loaded="OnOverviewSurfaceLoaded"
RenderableSeries="{Binding ElementName=ChartSurface, Path=RenderableSeries}"
YAxes="{sc:AxesBinding YAxes}">
<sc:SciChartSurface.XAxis>
<sc:NumericAxis VisibleRange="{Binding VisibleRange}" DrawMajorGridLines="False" DrawMinorGridLines="False" DrawMajorBands="False" Visibility="Collapsed"/>
</sc:SciChartSurface.XAxis>
<sc:SciChartSurface.Annotations>
<sc:VerticalLineAnnotation VerticalAlignment="Stretch"
IsEditable="True"
ShowLabel="False"
Stroke="#FF42b649"
StrokeThickness="4"
YAxisId="ChannelA"
X1="{Binding SelectedIndex, Mode=TwoWay}" />
</sc:SciChartSurface.Annotations>
</sc:SciChartSurface>
I expect that when I drag the Annotation along the horizontal length of the chart that I will get a value corresponding with the X position placed in SelectedIndex, but all I ever get is zero.
The cause of this issue turned out to be the data type of SelectedIndex, which was an unsigned integer. Despite the fact that VerticalLineAnnotation's X/Y value type is IComparable, if you are using the IsEditable="True" function, the drag step size is fractional and if bound to an integer type, it will continually round to 0 making it incapable of movement.
If there is a way to change the step size of the drag in SciChart, I was unable to locate it. Instead I changed SelectedIndex to a double and rounded it to my desired unsigned integer in the setter, which solved my problem.

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

Binding with a converter inside a template

I have defined a DataTemplate for a ListBox. Inside the template, I use TextBlock controls to display the properties of the data context. For example:
<TextBlock Text="{Binding Path=FirstName}" />
And if I do this:
<TextBlock Visibility="{Binding Path=IsAccountValid}" />
...the application runs, but there is a warning in the output about trying to bind a boolean property to a Visibility enumeration.
If I do this:
<TextBlock Visibility="{Binding Path=IsAccountValid,Converter={StaticResource visibilityOfBool}}" />
and somewhere in my App.xaml is:
<BooleanToVisibilityConverter x:Key="visibilityOfBool" />
I get a null reference exception.
I suspected this might be because the property IsAccountValid is not a dependency property, so I added a CheckBox to the window, and did this:
<TextBlock Visibility="{Binding Path=IsChecked,Converter={StaticResource visibilityOfBool},ElementName=butA}" />
But got the same error.
Why? The DataContext object is valid because if I bind IsAccountValid to the Text property, the value is correctly displayed.
The converter is never called, so I am wondering if it is the converter that cannot be found.
Why can the converter not be found? Why can the converter be found outside the data template and not inside the data template?
I tried building the template again with Blend, as Blend usually gets it right, but the code it generated was the same as mine.
I tried some of the fixes suggested on this website, including setting RelativeSource to TemplateParent and Self, but it made no difference.
What is going on?
We use such converter all the time in our Data Templates. Do you define the converter key inside your resource dictionary? Merge another Resource Dictionary?
The IsAccountValid property doesn't have to be a dependency property. If the converter couldn't be found then you wouldn't be able to open the form. You have the right approach using the converter but it is difficult to say exactly what is causing the exception without seeing more information.
As Amittai and Chris pointed out, it seems that you're headed in the right direction. I know it sounds a bit stupid, but try to add a space between the comma and the Converter= statement in the binding. Like so:
<TextBlock Visibility="{Binding Path=IsAccountValid, Converter={StaticResource visibilityOfBool}}" />
On some systems there are weird symptoms when there's no space after the comma. I couldn't find the actual reason for that.
Thank you all for your help in my investigation.
I have solved the problem.
These two lines of code are included in the DataTemplate, one is a TextBlock, and one is a hyperlink:
<TextBlock Text="Hello" Visibility="{Binding IsChecked,ElementName=chkBox,Converter={StaticResource visibilityOfBool}}" />
and
<TextBlock Grid.Column="1" >
<Hyperlink Click="ProgHomePageHyperlink_Click" >
<TextBlock Text="{Binding Path=Title}" />
</Hyperlink>
</TextBlock>
When they are both included in the code, the runtime throws a null reference exception.
But if I comment one of them out, either the TextBlock or the HyperLink, everything runs ok.
If I remove the Click handler from the hyperlink, everything runs ok.
If I comment out the converter in the TextBlock, the application runs, but I get a mismatched binding warning, which is well deserved.
So, including a Click handler in the hyperlink means the converter in the TextBlock cannot be found.
How weird is that!

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