简体   繁体   中英

Convert string to int using IValueConverter

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();
}

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM