0

How do I convert string values to integers and back using IValueConverter?

  • I have a database that consists of two tables; table CompanyX and table DeptY.
  • Table CompanyX has field ID(int), firstName, lastName, Email, Phone.
  • Table DeptY has field pID(int), Roles.
  • DeptY pID is foreign key To CompanyX ID. Every time I select someone in the Combobox, I want it to display as their ID in a DataGrid.

This is my ItemTemplate below:

<Application.Resources>
    <DataTemplate x:Key="myTemplate">
        <WrapPanel HorizontalAlignment="Stretch">
            <TextBlock Text="{Binding FirstName}"/>
            <Label />
            <TextBlock Text="{Binding LastName}"/>
        </WrapPanel>
    </DataTemplate>
</Application.Resources>

This is my Combobox which is bound to the ItemTemplate:

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,90,267,0" 
          Name="comboID"    ItemsSource="{Binding}" VerticalAlignment="Top" 
          Width="208" ItemTemplate="{StaticResource myTemplate}" />

And a DataGrid which displays:

<DataGridTemplateColumn x:Name="pIDColumn" Header="Person ID" Width="auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=pID, Converter= {StaticResource myConverter}}"/>   
        <DataTemplate>
    </DataGridTemplateColumn.CellTemplate>                            
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="rolesColumn" Header="Roles" Width="auto" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Roles}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> 

IValueConverter which is not converting!!

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    string a = (string)value;
    int b;
    int.TryParse(a, out b);
    return b;
}
public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
    throw new NotImplementedException();
}
Zong
  • 6,160
  • 5
  • 32
  • 46
AndyRoxxx
  • 19
  • 1
  • 1
  • 5

2 Answers2

3

You are converting the wrong way around. Convert() takes the binding source as input (int in your case), and outputs what the xaml is expecting (string).

But you don't even need a converter. You can bind straight to an int and WPF will automatically call ToString() on it to display it as text.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
  • How would you do that?? – AndyRoxxx May 09 '14 at 13:51
  • I don't want to display it as text. I want to display in the datagrid the ID of that person I'm selecting in the comboBox, e.g. when I click the add button, it should display The persons' ID. – AndyRoxxx May 09 '14 at 14:13
  • @Andy What do you want to display it as then? the only other options are either picture(drawing) or smoke signals. – XAMlMAX May 09 '14 at 14:18
  • Now if you want to display it as an ID - let's assume you mean TextBlock-uneditable text on the screen, then get rid of the converter and just bind OneWay.i.e. `{Binding Path=ID, Mode=OneWay}` – XAMlMAX May 09 '14 at 14:39
  • Sorry XAMIMAX, May be I should re-explain the problem that – AndyRoxxx May 09 '14 at 21:01
  • Sorry XAMIMAX, May be I should re-explain the problem that I am actually having. I'm tying to display the pID which is table DeptY – AndyRoxxx May 09 '14 at 21:05
  • Sorry XAMIMAX, May be I should re-explain the problem that I am actually having. I'm tying to display the pID which is table DeptY, I have also tried {Binding Path=pID} with no success and I get an error message "Conversion failed when converting the nvarchar value 'System.Data.DataRowView' to data type int". – AndyRoxxx May 09 '14 at 21:10
  • And also tried your solution with that same message, that's why I'm assuming it might need a converter. – AndyRoxxx May 09 '14 at 21:22
  • Will the other way round be automatic too? (String to int) – Hans GD May 26 '23 at 22:34
0

Like was said in the other answer, you are doing it backwards. The Convert side of an IValueConverter is meant for converting from the source property to what is in the bound element, in your case a textbox. You need to do your parsing on the ConvertBack side because that is what takes what is in the textbox (a string) and converts it back to a number.

public class IntToString : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int ret = 0;
        return int.TryParse((string)value, out ret) ? ret : 0;
    }
}
Wobbles
  • 3,033
  • 1
  • 25
  • 51