Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging binding property type from string to int
    primarykey
    data
    text
    <p>I'm trying to change the binding property of several columns from string to int since they are being read from a file and I want to use numeric sort when the data is finally displayed on screen.</p> <p>I created this converter and made the corresponding changes in the XAML file to implement the converter in the <code>DataTemplate</code> tag but I am still getting string sort in my output, not numeric sort. </p> <p>I looked around for other versions of a similar converter but didn't find anything relevant. I'm unsure of what the problem with my converter is. </p> <p>Here is my converter:</p> <pre><code>public class IntToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is string) { string s = (string)value; int num; if (int.TryParse(s, out num)) return num; } return 0; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { int number = (int)value; return number.ToString(); } } </code></pre> <p>And these are the changes made in the <code>XAML</code>. The local <code>namespace</code> is defined in <code>Windows.Resources</code>.</p> <pre><code>&lt;me:IntToStringConverter x:Key="IntToStringConverter" /&gt; </code></pre> <p>The sorting part looks like this:</p> <pre><code>&lt;toolkit:DataGridTemplateColumn Width="55" Header="Min Run" SortMemberPath="MinRun"&gt; &lt;toolkit:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{ Binding Path=MinRun, Converter={StaticResource IntToStringConverter}}" HorizontalAlignment="Center" /&gt; &lt;/DataTemplate&gt; &lt;/toolkit:DataGridTemplateColumn.CellTemplate&gt; &lt;/toolkit:DataGridTemplateColumn&gt; </code></pre> <p><code>DataGrid</code> is bound here:</p> <pre><code>&lt;toolkit:DataGrid x:Name="myGrid" Grid.Row="1" Background="{x:Null}" Foreground="White" ItemsSource="{Binding ObsRacResults}" VirtualizingStackPanel.IsVirtualizing="True" BorderThickness="0" Margin="4" VerticalGridLinesBrush="#FF555555" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False" HorizontalGridLinesBrush="#FF555555" SnapsToDevicePixels="True" CanUserResizeRows="False"&gt; </code></pre> <p>and then I have my columns, styles etc. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload