2

have a datatable which is being inserted with doubl.NaN when we dont have values. What i want to do is whenever i see double.Nan i want to put "-" instead, therefore i have a converter with the following code:

        if (value.ToString() == double.NaN.ToString())
        {
            return "-";
        }

everything worked perfectly on "en-us" culture, but when we use different culture (like "zn-cn") it doesnt work, due to the fact that "double.NaN.ToString()" results with a chinese word where value.ToString() is the normal "NaN".

i tried using double.parse() using the converters, nothing worked.

Update - ok the problem is that the converter gets the object as string and not as double this is the Xaml:

  <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border Background="Transparent"  Padding="10 8" BorderBrush="Transparent"  BorderThickness="1">
                    <Border.Resources>
                        <Style TargetType="ToolTip">
                            <Setter Property="OverridesDefaultStyle" Value="true"/>
                            <Setter Property="HasDropShadow" Value="False"/>
                            <Setter Property="VerticalOffset" Value="-46"/>
                            <Setter Property="HorizontalOffset" Value="10"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ToolTip">
                                        <General:TextTooltip TitleStyle="{StaticResource NumericTableToolTipTitleStyle}"  Content="{TemplateBinding Content}" ContentStringFormat="G"  ContentStyle="{StaticResource NumericTableToolTipContentStyle}"/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Border.Resources>
                    <TextBlock x:Name="tbCellText" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                               Path=Content.Text, Converter={StaticResource NumericTableCellContentConverter}, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}"
                               ToolTip="{Binding ElementName=tbCellText, Path=Text}"
                               TextWrapping="Wrap"/>
                </Border>
            </ControlTemplate>

is there any way to preserve the type of the object?

i know i could just do a comparison with "NaN" but i would like more generic solution for that matter.

Ori Price
  • 3,593
  • 2
  • 22
  • 37

5 Answers5

2

When you bind to a property of type double the converter gets a value parameter of type double. You could easily do this:

if (double.IsNaN((double)value))
{
    return "-";
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • thanks, but i tried it and it doesnt work. the value cannot be converted to double.... – Ori Price Jul 19 '12 at 14:32
  • i thinks its because the value object is being converted to string ("NaN") somehow – Ori Price Jul 19 '12 at 14:36
  • Isn't the property you're binding to of type `double`? If it's a string that's always "NaN" when the value is `NaN`, your comparison could be as simple as `if (value as string == "NaN")`. – Clemens Jul 19 '12 at 14:38
  • i am inserting it to a datatable (there its a double) then i am binding the datatable to a datagrid in the xaml with the relevant converter. i would expect that the value that arrive to the converter will stay the same (double) but apperantly it doesnt.... i dont want to relay on strings... is there any way to convert the string "NAN" to double? – Ori Price Jul 19 '12 at 14:46
  • With "relevant converter" you mean the converter we're talking about here? If the property that you bind to is of type `double`, then the converter gets a `value` parameter of type `double`. If that's not the case, you have to find out why. Perhaps you post the relevant XAML (with the binding). – Clemens Jul 19 '12 at 14:51
  • i've confirmed that its actually a string so you are right and i think the problem is that its being converted to string in the control template. – Ori Price Jul 19 '12 at 14:55
  • maybe the problem is that i am converting the value AFTER binding the data table to a data grid and when extracting the value from data grid would always be a string? – Ori Price Jul 19 '12 at 15:04
  • Yes indeed, you should somehow bind directly to the data. – Clemens Jul 19 '12 at 15:06
1

if (double.IsNaN(value)) ... will do in any locale.

Documentation link: http://msdn.microsoft.com/en-us/library/system.double.isnan.aspx

BTW, direct comparison to double.NaN (if (value == double.NaN)) won't work, as the comparison of NaN to NaN returns false! You could however detect NaN by such a trick: if (value != value) ... (this holds true only for NaNs). But an explicit test double.IsNaN is more readable.

Edit: as other answers suggest, inside the converter your value is perhaps not double, but object. So you'd need to include a check:

if (!(value is double))
    return ""; // or signal an error, or whatever
double d = (double)value;
if (double.IsNaN(d))
    return "-";
...
Vlad
  • 35,022
  • 6
  • 77
  • 199
1

One little addition to Vlad:

if (value is double && double.IsNaN((double)value))

Alexander Efimov
  • 2,685
  • 3
  • 18
  • 22
0

Don't bind to the content.text property in your datagridcell. That's why the value is coming in as a string.

Try storing the data value in the Tag property of the TextBlock and bind to that, then in property updated code, set the value of the text.

Lee Louviere
  • 5,162
  • 30
  • 54
0

Solution - the problem is that i was binding the data table to a datagrid and only after that i am converting all values. Extracting values from data grid is always of type strig so there was my problem. i just need to use the converter while binding directly to the data table instead to datagrid cells.

Ori Price
  • 3,593
  • 2
  • 22
  • 37